Android Question UDP send/receive not working

davemorris

Active Member
Licensed User
Longtime User
Hi, Guys
I have a problem with getting the UDP send/receive to work when implementing the example given in the Network documentation for UDPsocket. see https://www.b4x.com/android/help/network.html#udpsocket

I put the example into a simple project (attached) but when I run it on my phone the UPD_PacketArrived() event does not trigger (its associated msgbox is not shown and does not hit the breakpoint in debug). I have tried installing the software on multiple phones but none appear to work.

Background: I have an App which uses standard server/client LAN operation and communicates successfully using TCP (for reliability). However, the user must enter the server IP address into the client before the App can start to communicate. I would like to make this process automatic. So if the Server end sends its IP address periodically using UDP, then hopefully the client will pick it up and switch to TCP to start the communications process.

Assuming however it is possible for a device to transmit its IP address using UDP which can be received by all listening devices, if not, its back to the drawing board!

Any input would be appreciated.

Kind regards
Dave
 

Attachments

  • UDP_Test.zip
    9.2 KB · Views: 46
Solution
try it this way. also, make sure about your device's ip address. 10.0.0.1 sounds like it would be your router, not a simple android device. i used my device's local address.

drgottjr

Expert
Licensed User
Longtime User
try it this way. also, make sure about your device's ip address. 10.0.0.1 sounds like it would be your router, not a simple android device. i used my device's local address.
 

Attachments

  • udp.png
    udp.png
    44.4 KB · Views: 59
  • udp_test2.zip
    9.3 KB · Views: 64
Last edited:
Upvote 0
Solution

adelineana

New Member
Hi Dave, Ensure your firewall allows UDP traffic on the specified port. Double-check the code for any errors. Try a different port. Verify that the phones are on the same network. Consider using a broadcast address. If issues persist, consult the community forums for further assistance.
 
Upvote 0

davemorris

Active Member
Licensed User
Longtime User
try it this way. also, make sure about your device's ip address. 10.0.0.1 sounds like it would be your router, not a simple android device. i used my device's local address.
The attached example in post #2 works and has been marked as the solution:

Just a quick note:
I changed the embedded address (from 10.0.0.1) to my networks's broadcast address (in my case 192.168.0.255) compiled the code and installed the release version onto 3 phones. They worked as expected except one phone only received a few messages. I changed the ports from 5000 and 5001 to 50000 and 50001 respectively and reinstalled and they all worked reliably - I do believe ports below 49153 tend to be reserved, perhaps there was a contention issue with 5000 and 5001 for one of my phones.

Anyway thanks for the input - I do believe someone needs to update the sample code in the document reference in post #1 as I believe it is misleading.

Regards
Dave
 
Last edited:
Upvote 0
Top