I have a thread which periodically sends a datagram packet with the following setup:
DatagramSocket mySocket;
try {
mySocket = = new DatagramSocket(9999);
mySocket.connect(new InetSocketAddress(dstAddress, dstPort));
} catch (SocketException e) {
e.printStackTrace();
return;[![enter image description here][1]][1]
}
byte[] sentPacketBuffer = new byte[1];
DatagramPacket sentPacket = new DatagramPacket(sentPacketBuffer, sentPacketBuffer.length);
For each call of the send
method:
mySocket.send(sentPacket);
I get a different source port
on the receiver side.
I'v looked into this question, but the answer is actually related to setting the source port for the listener side.
Is there a way to make the source port (of the sender) persistent?
Edit
I used Android's VPNService to capture the received packets, and I dumped them to Wireshark:
As you can see only 1 packet has the correct source port.
Then I figured it might be related to the destination IP. The destination IP is not reachable from this device.
If I do make this address reachable (by connecting to 192.168.49.1, and having an interface in the same subnet) I get correct source port for all packets:
So, my question is now why does the destination reach-ability (or available interfaces) is related to the source port?