I am TRYING to send UDP Packets back and forth from a B4J Client to a B4R Server (ESP8266 is the Server ).
The ESP8266 is set to an AP and I connect the B4J client to the ESP8266 SSID which I set. That all works.
I can send from the B4J Client and Receive on the B4R side. Then I take the received Objects and try to sent them back to the B4J Client.
The B4J Client Receives the UDP Packet but I cannot figure out how to decode the Objects from the packet. I control both sides. The data sent from B4J is just an example of what my "real" App might send. I looked at all the examples on the forum but I can't find one for this use case.
Any help decoding the incoming UDP Packet on B4J would be appreciated.
I know their are other "better" solutions, but if anyone can give me pointer on how to get this working that would be great. Thanks !!!
B4R Server Code ...
B4J Client Send and Receive Code
Updated the Title so I can find it next year! LOL
The ESP8266 is set to an AP and I connect the B4J client to the ESP8266 SSID which I set. That all works.
I can send from the B4J Client and Receive on the B4R side. Then I take the received Objects and try to sent them back to the B4J Client.
The B4J Client Receives the UDP Packet but I cannot figure out how to decode the Objects from the packet. I control both sides. The data sent from B4J is just an example of what my "real" App might send. I looked at all the examples on the forum but I can't find one for this use case.
Any help decoding the incoming UDP Packet on B4J would be appreciated.
I know their are other "better" solutions, but if anyone can give me pointer on how to get this working that would be great. Thanks !!!
B4R Server Code ...
B4X:
Sub Udp_PacketArrived (Buffer() As Byte, ip() As Byte, port As UInt)
' Log("PacketArrived ", Buffer.Length," Port= ", port," IP= ",ip(0),".", ip(1),".", ip(2),".", ip(3))
If Buffer.Length = 0 Then
Log("Packet Length 0 ")
Return 'invalid message
End If
If Buffer.Length > 50 Then
Log("Packet Length > 50 ")
Return 'invalid message
End If
'************************************************************
' Log("Buffer.Length= ",Buffer.Length)
Dim be(10) As Object 'used as a storage buffer. Can be a global variable
Dim Data() As Object = serz.ConvertBytesToArray(Buffer, be)
For Each o As Object In Data
Log(o)
Next
' Received a Packet Now Send One Back
Send_Packet(Data)
End Sub
Sub Send_Packet(Data()As Object)
'**********************************************************************
'''' IP/Port of Client "222.111.7.100" --- "55777"'''''''''''''''''''''
'**********************************************************************
Private ip() As Byte = Array As Byte(222,111,7,100)
Private port As UInt = 55777 'was 55999
udp.BeginPacket(ip, port)
udp.Write(serz.ConvertArrayToBytes(Data))
udp.SendPacket
Log("SentPckt")
End Sub
B4J Client Send and Receive Code
B4X:
Sub Button1_Click
build_Packet
End Sub
Sub build_Packet
Dim pktStart As Object = "<"
Dim x As Object = Rnd(0,255)
Dim y As Object = Rnd(0,255)
Dim myStr As Object = "string"
Dim pktEnd As Object = ">"
'
Dim send_data(5) As Object
send_data(0) = pktStart
send_data(1) = x
send_data(2) = y
send_data(3) = myStr
send_data(4) = pktEnd
udp_SendPacket(serz.ConvertArrayToBytes(send_data))
End Sub
Sub udp_SendPacket (xmit_data() As Byte)
Dim Packet As UDPPacket
Packet.Initialize(xmit_data, udp_Ip, udp_Port)
udpSocketX.Send(Packet)
Log("Packet Sent")
End Sub
Sub udp_PacketArrived (Packet As UDPPacket)
Log("Packet Arrived")
Log($"IN PL= ${Packet.Length} Port= ${Packet.Port} IP= ${Packet.HostAddress}"$)
If Packet.Length = 0 Then
Log("Packet Length 0 ")
End If
If Packet.Length > 50 Then
Log("Packet Length > 50 ")
End If
Dim msg As Object = bc.HexFromBytes(Packet.Data)
Log($"${msg}"$)
End Sub
' Log($"${msg}"$) which logs the info below ....
' Waiting For debugger To connect...
' Program started.
' Packet Sent
' Packet Arrived
' IN PL= 21 Port= 55777 IP= 222.111.7.1
'7E05083C00019E01A608737472696E6700083E007F...'{000.. to 1024(which is my Buffer Size)}
' 3C is "<" 3E is ">"
Updated the Title so I can find it next year! LOL
Last edited: