Android Question [SOLVED] Send UDP packet

Discussion in 'Android Questions' started by Vinians2006, Jul 13, 2019 at 10:45 PM.

  1. Vinians2006

    Vinians2006 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:
    Code:
    #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"6000255)
       
    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(010000000) & ""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.16000)
      
       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: Jul 13, 2019 at 11:49 PM
  2. Erel

    Erel Administrator Staff Member Licensed User

    What is your PC ip address? Is the UDP port open in the firewall?

    Where is the B4J code that listens for the incoming packets?
     
  3. Vinians2006

    Vinians2006 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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice