UDP Sockets

Bangdido

Member
Licensed User
Longtime User
Hello there

I am developing an application with UDP sockets.
this should shows diferent screens(activities)

if i create the udp socket in the main activitie and it is showing
other screen, the udp socket is not working, it is blocked till the main
thread gets the "focus" or is resumed.

can somebody teach me or post a simple program about how to use a non-blocking udp?

maybe in a service or a threads..
tnx in advanced
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
This is working for me
Past this in a service module


B4X:
#Region  Service Attributes 
   #StartAtBoot: False
#End Region

Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
     'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public UDPSck As UDPSocket   
    Private Tablet As Reflector
    'Dim Device As Phone
    'Dim ServerIndex As Int = -1
    'Dim RequestIP As Boolean = True
    Public UDPPort As Int = 4444
   Public myIP As String 
   
   Type typPeers (name As String, IP As String, role As String, dtime As Long,screens As String)
   Public peer As typPeers
   Public peers As Map
   

End Sub

Sub Service_Create
    Tablet.Target = Tablet.GetContext
    Tablet.Target = Tablet.RunMethod2("getSystemService", "wifi", "java.lang.String")
    Tablet.Target = Tablet.RunMethod2("createMulticastLock", "mylock", "java.lang.String")
    Tablet.RunMethod2("setReferenceCounted", False, "java.lang.boolean")     'not really necessary but safer
    Tablet.RunMethod("acquire")                                            'acquire the lock
    UDPSck.Initialize("UDP", UDPPort, 4096)    'Port and buffer size can be changed
    
   ToastMessageShow("Start UDP Engine -->  Port " & UDPPort & " initialized.",False)
   Dim server As ServerSocket : myIP = server.GetMyWifiIP
   
   peers.Initialize
End Sub




Sub Service_Start (StartingIntent As Intent)

End Sub




Sub UDP_PacketArrived(Packet As UDPPacket)
    Dim msg As String = BytesToString(Packet.Data, Packet.Offset, Packet.Length, "UTF8")
    Dim IP As String = Packet.HostAddress
    Dim parts() As String = Regex.Split("~", msg)
   Dim s As String = "Receiving: " & msg & "  from: " & IP
   Log(s)
   ToastMessageShow(s,True)
    Select Case parts(0)
   Case "BCAST_ALL"
          
          
        Case "WHOAMI"  
         
        Case "KALARM"
         If myIP = IP Then Exit 
         
      
    End Select
End Sub



Sub Service_Destroy
    UDPSck.Close
   '--- release when not needed to save battery    
    Tablet.RunMethod("release")
End Sub

to send a message

B4X:
    dim msg as string = "MESSAGE"
    '--- send a UDP message
    Log("UDP Sending: " & msg)
    Dim Packet As UDPPacket : data() As Byte  
    data = msg.GetBytes("UTF8")
    Packet.Initialize(data, "255.255.255.255", 4444)
    svrUDP.UDPSck.Send(Packet) '--- svrUDP is the NAME of the service
 
Upvote 0

Bangdido

Member
Licensed User
Longtime User
thank you JakeBullet70, too usesfull the example.

question: what are you doing with the object/var tablet ?
 
Last edited:
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Nevermind. I found it. Acquire wont run because something about user or process not having permission to run it?
 
Upvote 0

Similar Threads

Top