Android Question UDP packet sending

Nitin Joshi

Active Member
Licensed User
Hi, my APP is using UDP protocol to communicate with Esp8266. My APP needs to send UDP packets 2 or 3 times for successful transmission. This leads to malfunctioning.
Please guide me some codes lines to avoid multiple UDP packets transmission.
Note: I have to keep local port fix.
 

Nitin Joshi

Active Member
Licensed User
About project.
1. ESP8266 and APP are in same WiFi network.
2. APP communicating with ESP8266 on UDP protocol.
3. When APP sends a packet to device, device in returns sends acknowledgement packet.
4. When I send the packet from APP, device does not receive that UDP packet. I am using serial monitor of esp8266 to verify.
5. I have send UDP packet from APP for 2 or 3 times at least. I am using FOR loop for the same.
6. Sometimes device gets all packers some times 1 or 2 less. For example if I send packet 3 times, device receives 3 times or less. Later device sends acknowledgement packets that many times.
7. This is leading to malfunctioning in my APP.

What I am looking for.
UDP packet should get deliver in first attempt.

Hope now clear.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
This is the code I am using
Sub UDP_Send (Str As String, remoteIP As String)
Dim Data() As Byte
Dim i As Int
For i=1 To 3
If UDPSkt.IsInitialized=False Then
UDPSkt.Initialize("UDP", 4211, 255)
End If
Data=Str.GetBytes("UTF8")
UDPPkt.Initialize(Data, remoteIP, 4210)
Sleep(500)
UDPSkt.Send(UDPPkt)
Next
End Sub
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Thanks for advice, but I can not switch back to other software. Please suggest to my original query...why do i need to send UDP message more than 1 time?
Plenty of folks here have used B4A for sending/receiving UDP packets without issue. So either 1) your target IP and/or port is wrong, 2) your device is not picking up the UDP packet, or 3) you have a really crappy network connection. Most likely it's #2. I guess you could put a log in your For/Next loop to see it execute 3 times (or debug it). If it sent out the 3 packets and you're only getting 1 or 2, then it is on the other device end (or you have a really bad network).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
What I am looking for.
UDP packet should get deliver in first attempt.
BTW, you realize that UDP is not guaranteed. That's the whole point of UDP, it trades speed for guaranteed delivery.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
If you need guaranteed delivery, you should use TCP. I have a B4A/B4R system that sends/receives large amounts of data to/from an ESP8266 at 2Hz to 5Hz via TCP without any problems. For the times that there is no data being sent/received, I use a watchdog timer to keep the connection alive.

- Colin.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Oh thanks. Unfortunately, I can not revise my solution to TCP. Meanwhile, I am looking a solution to check connection is live or not. Can I use watchdog timer? Please help with more details..
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Why are you initializing your UDP socket & packet on different ports?

B4X:
UDPSkt.Initialize("UDP", 4211, 255)
UDPPkt.Initialize(Data, remoteIP, 4210)

Shouldn't they both be on the same port?

Eg: here's the initialization code I use in one of my apps that uses UDP broadcast to find other instances of the app on the network, then switches to TCP. It works 100% of the time (except when the network has UDP broadcast disabled).

B4X:
Private UDPPort As Int = 13130
UDPSocket1.Initialize("UDP", UDPPort, 256)
Packet.Initialize(data, IPAddr, UDPPort)

- Colin.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Please see my comment beside command.
B4X:
UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port
UDPPkt.Initialize(Data, remoteIP, 4210)  'here 4210 is remote port.

Am I wrong in above code?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Meanwhile, I am looking a solution to check connection is live or not.
UDP is a connection less protocol. UDP should be used in cases where packet loss (up to a certain point) is not an issue (such is VOIP and Video). In your case, it looks like packet loss is detrimental to your application. Therefore, you have to build your own retry mechanism (which TCP has already build in). Instead of just sending out x number of packets and then expecting a response, you need to send out each packet individually, and then wait for each packets response before sending the next one*. Worse, you have to somehow ID the packets, since acknowledgements may come out of order. This is all the stuff that TCP does already for you and that you have to manage yourself when using UDP.

(*) You can also sent out a group of packets and then wait for one response that list the packets that were received on only resend packets that seem to have been lost. Once more, this is something that TCP does for you.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Please see my comment beside command.
B4X:
UDPSkt.Initialize("UDP", 4211, 255) ' here 4211 is local port
UDPPkt.Initialize(Data, remoteIP, 4210)  'here 4210 is remote port.

Am I wrong in above code?

Looking at the example in the UDPSocket documentation, I guess it should work using 2 different ports (assuming you are sending / receiving on the correct ports at the other end). I've always just used a single port for UDP (which also works fine), so it seemed odd to me...

- Colin.
 
Upvote 0
Top