Looking for UDP example for sending a string

Hamo

Member
Licensed User
Longtime User
Can anyone please give me an example of sending a string from one android device to another using UDP. Thanks
Hamo
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Process_Globals
   Dim usocket As UDPSocket
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      usocket.Initialize("usocket", 0, 0)
   End If
End Sub

Sub SendString(ip As String, port As Int, s As String)
   Dim u As UDPPacket
   u.Initialize(s.GetBytes("UTF8"), ip, port)
   usocket.Send(u)
End Sub
 
Upvote 0

Hamo

Member
Licensed User
Longtime User
Thanks Erel, I got it working, sending a UDP broadcast message to all 6 tablets.
Hamo
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
I'm curious... when sending out a UDP packet does it need to contain certain data or be sent to a certain port in order for certain devices to respond to it? I was trying to do this the other day but had no luck. Wireshark showed UDP packets leaving my app but nothing ever seemed to respond. I was following other sample code on this forum and even added broadcast packet receiver code (through reflection) which added the WiFi permission to receive IP broadcast packets.

I never figured out what I was doing wrong, but seeing this thread has me wondering if perhaps I can take another shot at it.
 
Upvote 0
Top