B4J Question updpacket not received at specified port...bug?

Dan Haugland

Member
Licensed User
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?

I'm using jnetwork v1.2
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi Dan,

attached an example UDP client to client which might help.
In this example:
Client 1 = Broadcast Port = 3662
Client 1 = Listener Port = 3661

Client 2 = Broadcast Port = 3661
Client 2 = Listener Port = 3662
 

Attachments

  • B4JHowToUDPc2c.zip
    475.8 KB · Views: 330
Upvote 0

Dan Haugland

Member
Licensed User
Hi Dan,

attached an example UDP client to client which might help.
In this example:
Client 1 = Broadcast Port = 3662
Client 1 = Listener Port = 3661

Client 2 = Broadcast Port = 3661
Client 2 = Listener Port = 3662

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. :)
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

think you are right - see screenshot of the B4J HowTo UDP Example - would have expected the received information:
  • (left dialog) for client 1 from client 2 on port 3661 as the client 2 broadcast port is 3661 instead of shown 3662
  • (right dialog) for client 2 from client 1 on port 3662 as the client 1 broadcast port is 3662 instead of shown 3661
means in UDP_PacketArrived (Packet As UDPPacket) the Packet.Port, as the receiver port, is probably wrong (swapped)

upload_2016-5-5_10-23-3.png
 
Upvote 0

Dan Haugland

Member
Licensed User
Computer A sends a packet to computer B.

UDPPacket.Port will return the port number of computer A. Not computer B socket port.

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.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4A code:
B4X:
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

1111 is the correct port.
 
Upvote 0

rwblinn

Well-Known Member
Licensed User
Longtime User
I concur - many thanks for the example provided.
 
Upvote 0
Top