Android Question UDP send/receive problem

Discussion in 'Android Questions' started by mirekk36, Aug 31, 2015.

Similar threads

B4A Tutorial MQTT Chat with Auto Discovery
B4A Code Snippet UDP Broadcast Address
B4A Tutorial [B4X] AsyncStreams Tutorial
B4A Question UDP Packet Receiving
B4A Question UDP packet sending
B4A Question Sending UDP packets
  1. mirekk36

    mirekk36 Member Licensed 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:
    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 & 

    "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(
        udppack.Initialize( data, ip, port )
        udpsrv.Send( udppack )
    End Sub
  2. Computersmith64

    Computersmith64 Well-Known Member Licensed 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 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.
  3. mirekk36

    mirekk36 Member Licensed User

    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 ...

    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
  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