Android Question [SOLVED] Send UDP packet

Vinians2006

Active Member
Licensed User
Hello everyone, I am trying to send UDP packages from my Android device and it works the first time but then it stops. I know UDP packages are not garanted to arrive but to test I made a Java desktop UDP client and a sever and when I use the java client I made its works perfectly very package arrives. The Android device is in the same network as the desktop where the server is running. I am using a code like that:
B4X:
#Region  Activity Attributes
   #FullScreen: true
   #IncludeTitle: false
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim usocket As UDPSocket

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Private btn0                As Button  
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Do not forget to load the layout file created with the visual designer. For example:
  
   If (FirstTime) Then
      usocket.Initialize("UDP", 6000, 255)
   End If

   Activity.LoadLayout("layoutScan")
End Sub


Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
  
End Sub

'---------------------------------------------------------
'Clique dos botoes numéricos
'---------------------------------------------------------
Sub btn0_Click
      UDPEnviaString("33333;1;1")
End Sub

Sub UDPEnviaString(msg As String)

   Dim Data()    As Byte
   Dim chave    As String
   Dim  udpip   As String
  
  
   chave = U.PadZero(Rnd(0, 10000000) & "", 10)
  
   msg  = chave & ";" & msg
   Data = msg.GetBytes("UTF8")

   Dim Packet As UDPPacket
   udpip = G.Config.UDPIp
   If (udpip = "") Then
       udpip = G.GetBroadcastAddress
   End If
  
   Packet.Initialize(Data, 192.168.1.1, 6000)
  
   usocket.Send(Packet)
  
  
End Sub

Sub UDP_PacketArrived (Packet As UDPPacket)
   Dim msg As String
   msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   ToastMessageShow(msg, True)
End Sub
I tryed using an IP address and a broadcast adress they both are facing the same problem. And the weid thing is that the sub UPD_PacketArrived is called! But the package dont arrive to the server...

Please, I tryed a day to find this bug but I cant see what is wrong. Can someone help me ?
 
Last edited:

Vinians2006

Active Member
Licensed User
Thank you Erel for your reply. It was my fault, I was using a wrong IP address, because my pc have a virtual ethernet created by Virutal Box and I was sending the package to that IP. My fault.
 
Top