Android Question UDP Broadcasting - data not receive

locxtronic

Member
Licensed User
Longtime User
Hi, im working on simple udp communication. I use the broadcast IP which is 255.255.255.255 to broadcast.
The problem is, it seem like the data is sometime received and sometime its not. I know that UDP dont have guarantee of data received. But i dont think it the main problem. because when i uninstall and re-install the app it working fine. I try it several times. Sometime the app is only receiving the data, and sometime it only transmitting the data. here is the code

B4X:
#Region  Project Attributes
    #ApplicationLabel: UDP Connection
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#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 UDPSocket1 As UDPSocket
    Dim send_packet As UDPPacket
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.
   
    Dim data() As Byte
    Dim ip_box As EditText
    Dim rx_msg As EditText
    Dim rx_port As EditText
    Dim tx_msg As EditText
    Dim tx_port As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub start_Click
    UDPSocket1.Initialize("UDP", rx_port.Text, 8000)
End Sub
Sub send_Click
    Dim msg As String
    msg = tx_msg.Text
    data = msg.GetBytes("UTF8")
    send_packet.Initialize(data,ip_box.Text,tx_port.Text)
    UDPSocket1.Send(send_packet)
End Sub
Sub UDP_PacketArrived(Packet As UDPPacket)
    Dim msg As String
    msg = BytesToString(Packet.data, Packet.Offset, Packet.Length, "UTF8")
    rx_msg.Text = rx_msg.Text & msg
    rx_msg.Text = rx_msg.Text & Chr(13)
End Sub

in this pic im trying to communicate 2 phones with a PC and the problem happen again,phone at the right is not receiving the number i sent from the pc

Untitled.png
 
Top