Android Question UDP send/receive problem

mirekk36

Member
Licensed User
Longtime User
I have small question, If I send some text from my PC to my android B4A application via simple UDP , sometimes there are some strange delays . So if I send data, sometimes I have to wait even up to about 1-2 seconds! before data shows in b4a app in edittext.text.

my code:
B4X:
Sub udpsrv_PacketArrived (Packet As UDPPacket)
       
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
   
    msg = "["&Packet.HostAddress&"] " & msg

    EditText1.Text = EditText1.Text & msg & CRLF

    udp_send( "some answer"&Chr(13)&Chr(10), Packet.HostAddress, Packet.Port )

End Sub

Sub udp_send( dane As String, ip As String, port As Int )
    Dim udppack As UDPPacket
    Dim data() As Byte
    data = dane.GetBytes("UTF8")
    udppack.Initialize( data, ip, port )
    udpsrv.Send( udppack )
End Sub
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
I have small question, If I send some text from my PC to my android B4A application via simple UDP , sometimes there are some strange delays . So if I send data, sometimes I have to wait even up to about 1-2 seconds! before data shows in b4a app in edittext.text.

my code:
B4X:
Sub udpsrv_PacketArrived (Packet As UDPPacket)
      
    Dim msg As String
    msg = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
  
    msg = "["&Packet.HostAddress&"] " & msg

    EditText1.Text = EditText1.Text & msg & CRLF

    udp_send( "some answer"&Chr(13)&Chr(10), Packet.HostAddress, Packet.Port )

End Sub

Sub udp_send( dane As String, ip As String, port As Int )
    Dim udppack As UDPPacket
    Dim data() As Byte
    data = dane.GetBytes("UTF8")
    udppack.Initialize( data, ip, port )
    udpsrv.Send( udppack )
End Sub

It could be just delays on your local network. I sometimes have the same issue playing Five Dice! with my wife on our home WiFi sitting right next to each other. Most of the time it's fine, but occasionally there will be a delay in updating between the 2 devices.

Just as an aside, using UDP may not be the best solution as there is no guarantee that the data will get to where it's going & no automatic retry if there is an issue. TCP is a much safer (albeit arguably slower) solution. I use UDP to establish a connection, then switch to TCP for the actual communication between the devices.

- Colin.
 
Upvote 0

mirekk36

Member
Licensed User
Longtime User
It could be just delays on your local network. I sometimes have the same issue playing Five Dice! with my wife on our home WiFi sitting
Yes you are right, it can be ... because after I have tested it with PC to PC on copper network ... there was not any, even small delays. Today I will test PC to my notebook connected via the same WIFI router ...

using UDP may not be the best solution as there is no guarantee that the data will get to where it's going & no automatic retry if there is an issue.
Yes I know about it, but in most my projects there it's best and fastest medium, especially if I do data transfer with my AVR microcontrollers. In such situation UDP is effortless solutions.

thx for your reply - because before I forgot that it can be wifi problem
 
Upvote 0
Top