B4J Question how to determine a UDPsocket is still open?

MbedAndroid

Well-Known Member
Licensed User
Longtime User
B4X:
Private Sub BroadcastTimer_Tick
    Try
'    Log(address)

    If address <> "" Then
    
        If autodiscover.IsInitialized=False Then autodiscover.Initialize("autodiscover",discoverPort , 8192)
        Private up As UDPPacket
        up.Initialize("Domotica".GetBytes("UTF8"), address, discoverPort)
        
        autodiscover.Send(up)
        autodiscover.close
    End If
    Catch
        Log("catch in Broadcasttimer_tick")
    End Try
End Sub

I'm running 2 domotica systems. Since i implemented MQTT i got the problem that the broadcasttimer crashes. Mostly exactly on midnight. 1 system never had this issue. The other it can happen every week. Both running the same code. The broadcasttimer fires every 4 seconds. When the broadcasttimer crashes, all timers are blocked thus the system isnt operational anymore.
the crash pointed at the broadcasttimer - see log below.
Translation "adres is al in gebruik" means this address is in use already. This message comes from java, not b4j code.

main.main (java line: 28)
java.lang.RuntimeException: java.lang.RuntimeException: java.net.BindException: Adres is al in gebruik (Bind failed)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:138)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.main(main.java:28)
Caused by: java.lang.RuntimeException: java.net.BindException: Adres is al in gebruik (Bind failed)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:138)
at anywheresoftware.b4a.objects.Timer$TickTack$1.run(Timer.java:135)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:47)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:43)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:175)
at b4j.example.main._appstart(main.java:929)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
... 2 more
Caused by: java.net.BindException: Adres is al in gebruik (Bind failed)
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:93)
at java.net.DatagramSocket.bind(DatagramSocket.java:392)
at java.net.DatagramSocket.<init>(DatagramSocket.java:242)
at java.net.DatagramSocket.<init>(DatagramSocket.java:299)
at java.net.DatagramSocket.<init>(DatagramSocket.java:271)
at anywheresoftware.b4a.objects.SocketWrapper$UDPSocket.Initialize(SocketWrapper.java:352)
at b4j.example.main._broadcasttimer_tick(main.java:947)
at sun.reflect.GeneratedMethodAccessor7.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
... 12 more

with adding a catch block the issue is caught, and the system works on.
On midnight the systems writes some data which was collected during the day, this writing takes time.
So it seems that the broadcasttimer was fired again when the previous udp transmission wasnt completed?

But how to check the UDPsocket isnt closed before the new message will be prepared and send?
 
Top