I have a UDPsocket initialized on port 56000, I send a udpPacket initialized with port=56142. Wireshark confirms that the srcport=56000 and dstport=56142 as expected. The receiving end has UDPsockets on 56000 and 56142. The packet is received on the 56000 socket, not the 56142. Is this a bug, or a user error?
Thanks for the example. If you add "&packet.port" to the end of the taPacketReceived.text assignment in UDP_PacketArrived I think you will find that the port that the message is received on is the send port, not the listen port. The packet on the sending side is addressed tot he listening port on the other client, but between the wire and _PacketArrived the source port and destination port are not being respected. I don't think I am misunderstanding the protocol rules, but I welcome en explanation to the contrary or link to same. Or a bug fix.
This is not true based on the wireshark capture. The packet in transit across the network only contains the proper destination port. There is no reference to the originating port number on computer A socket.
Sub Process_Globals
Private usocket As UDPSocket
Private const pcIP As String = "192.168.0.6"
Private const pcPort As Int = 51042
End Sub
Sub Globals
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
usocket.Initialize("usocket", 1111, 8192)
End If
End Sub
Sub Activity_Click
Dim up As UDPPacket
up.Initialize("sent from android".GetBytes("utf8"), pcIP, pcPort)
usocket.Send(up)
End Sub
B4J code (non-ui):
B4X:
Sub Process_Globals
Private usocket As UDPSocket
End Sub
Sub AppStart (Args() As String)
usocket.Initialize("usocket", 51042, 8192)
StartMessageLoop
End Sub
Sub usocket_PacketArrived (Packet As UDPPacket)
Log($"Received packet from ${Packet.Host}.
The packet message: ${BytesToString(Packet.Data, 0, Packet.Length, "utf8")}
The Packet was sent from port: ${Packet.Port} "$)
End Sub
The output in the B4J program:
Received packet from 192.168.0.13.
The packet message: sent from android
The Packet was sent from port: 1111