0

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:enter image description here

So, my question is now why does the destination reach-ability (or available interfaces) is related to the source port?

Community
  • 1
  • 1
noti
  • 887
  • 7
  • 25
  • The source port of datagrams sent via this code is always 9999. Unclear what you're asking. – user207421 Jan 03 '16 at 17:26
  • In the first case, where the destination IP isn't (directly) reachable, is whatever is making it reachable doing some form of NATing? – TripeHound Jan 04 '16 at 13:29
  • The thing that makes it reachable is only connecting to the relevant access point. In the first case the packet will never get to its destination. – noti Jan 04 '16 at 13:48
  • Are you sure your thread will not receive PortUnreachableException? Maybe it is silently recreated, but because previous socket is present, it will have to use different source port. Try setReuseAddress. Please include code also with sending the packet and exception handling around it. – Pihhan Feb 28 '17 at 10:23

1 Answers1

0

You are mistaken. The source port of datagrams sent by this code is always 9999.

NB Keep using the same socket. Creating and destroying a new one per datagram is pointless.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • The socket and the DatagramPacket are created only once. Only the send method is called repeatedly. – noti Jan 03 '16 at 17:23
  • In that case the source port *is* persistent, and your question is about a non-existent problem. – user207421 Jan 03 '16 at 17:25
  • Actually, in practice I get a different one for each call. – noti Jan 03 '16 at 17:29
  • Not with this code. Is this the real code? Or maybe you have a bug at the receiving end. Or maybe someone else is sending to your receiver. – user207421 Jan 03 '16 at 17:33
  • The source port of that datagram socket is 9999. That is the meaning of the code you posted. I don't have to try it to know that. – user207421 Jan 03 '16 at 17:35
  • Voting here is a secret ballot and you have no business speculating about it. – user207421 Jan 03 '16 at 17:37
  • Very nice. At least I would like to know why. – noti Jan 03 '16 at 17:39
  • I would like to see some evidence that the problem exists, such as the receiving code and its output. – user207421 Jan 03 '16 at 17:40
  • I think it would have been sufficient only to suggest that there is a bug on the receiver side, and actuallt it would have been an acceptable answer.Nevertheless, I'll check the receiver side once again and post my findings. – noti Jan 03 '16 at 17:48
  • I *did* suggest that. I have no idea what you're whinging about now. – user207421 Jan 03 '16 at 17:50