Android Question Send UDP Packet in Release

Blueforcer

Well-Known Member
Licensed User
Longtime User
i have a weird problem.

i send a UDP Package:

B4X:
Sub sendCommand(msg As String)
    Dim Packet As UDPPacket
    data = msg.GetBytes("UTF8")
    Packet.Initialize(data,GetBroadcastAddress,52829)
    
    For i = 1 To 3
        UDPSocket1.Send(Packet)
        Wait For (CheckResponse(2000)) Complete (Success As Boolean)
        If Success Then Exit
    Next
    If i = 4 Then
        Log("Failed to send...")
    End If
End Sub

It works great in Debug mode. But in Release i get an error:
java.lang.RuntimeException: java.lang.ClassCastException: anywheresoftware.b4a.objects.SocketWrapper$UDPSocket$MyDatagramPacket cannot be cast to anywheresoftware.b4a.objects.SocketWrapper$UDPSocket

so whats wrong?
 

OliverA

Expert
Licensed User
Which line causes the error? What is the code for CheckResponse? How are you calling sendCommand?
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Only UDPSocket1.Send(Packet) cause the error.
The code after will not be reached because of the error.
The code itself is ok i think. because it works completely in debug mode.
 
Upvote 0

OliverA

Expert
Licensed User
Try this:
B4X:
Sub sendCommand(msg As String)
    data = msg.GetBytes("UTF8")
    For i = 1 To 3
        Dim Packet As UDPPacket
        Packet.Initialize(data,GetBroadcastAddress,52829)
        UDPSocket1.Send(Packet)
        Wait For (CheckResponse(2000)) Complete (Success As Boolean)
        If Success Then Exit
    Next
    If i = 4 Then
        Log("Failed to send...")
    End If
End Sub
 
Upvote 0

OliverA

Expert
Licensed User
if possible upload you project (strip anything out not needed, but leave enough to repeat the issue). Do a Tools->Clean Project, then File->Export as Zip
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
Sure,
You will find the sendCommand sub in the starter.
It will try to send the message right after App start.
 

Attachments

  • awtrixApp (1).zip
    39.4 KB · Views: 190
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
java.lang.RuntimeException: java.lang.ClassCastException: anywheresoftware.b4a.objects.SocketWrapper$UDPSocket$MyDatagramPacket cannot be cast to anywheresoftware.b4a.objects.SocketWrapper$UDPSocket
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1663)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.ClassCastException: anywheresoftware.b4a.objects.SocketWrapper$UDPSocket$MyDatagramPacket cannot be cast to anywheresoftware.b4a.objects.SocketWrapper$UDPSocket
at de.awtrix.starter$ResumableSub_CheckResponse.resume(starter.java:224)
at anywheresoftware.b4a.keywords.Common$13.run(Common.java:1661)
... 7 more

It seems that the error only appears if there is an reciever in the network available. And only in Release mode
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Thank you for posting your project. It is line 65 (part of CheckResponse sub) in your Starter module that is causing you the issue.
B4X:
Dim packet As UDPSocket = result(0)
Do you see the issue (look at the variable name and then look at the class you are assigning to the variable)?

Note: I just found where you got the code for CheckResponse from (https://www.b4x.com/android/forum/threads/udp-acknowledge-wait-for-with-timeout.89185/#post-564283). To the author's credit, he did say it was untested. The cool thing is I learned something new about resumable subs. I did not know you did not have to use Wait For. Interesting...
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Upvote 0
Top