Search code examples
udpclient-serverportfirewallnat

If app A listens to the same private port previously used by app B, will app A get app B's public port number?


If one application listens to the same private port number previously used by another application on that computer, will the more recent app get the same public port number as the less recent app?

Application A and Application B both use non-multicast UDP. They both run on a computer that is behind a residential router that does not have port forwarding set up. If application A listens to the same private port number (behind NAT) that application B just finished listening to, will application A receive the same public port number that application B had when application B was using that port? Will application A get all the UDP packets meant for application B?

Another closely related question:

Imagine that computer A and computer B share a residential router. Computer A and computer B both run an application that sends a UDP packet/datagram to server C at public IP 123.456.789 and public port number 12345. Server C responds by sending a packet/datagram back to the senders. The application that computer A and computer B used, in addition to specifying that it wanted to send on port 12345, also listens on port 12345 for the reply. Computer A and computer B get the reply packet, but the public port number that the server, C, was sending its reply to might not match the number 12345 specified by the application that computer A and computer B are running (correct me if I am mistaken, please).

Now, for some reason, server C decides that it wants to kill the application that was sending and receiving udp packets to computers A and B and open up a completely different application that will send udp packets to the public IP's and public port numbers that the previous application used to communicate with computers A and B. Will the packets generated by this new server side application go through to computers A and B? Or will the death of the old application and the pause between killing the old application and running the new one cause the packets sent by the new server side application to be blocked or rejected by the clients? Will the packets make it through the router/NAT?

What if it was the other way around and computer B killed the application that it used to communicate with server C and then opened up a new, different application that internally also listens to udp port 12345. If server C sends another UDP response packet to computer B after computer B kills its old application, will this new, different application get the packets sent by server C that were meant to be a reply to the packets sent by the previous application run by computer B?

Does the answer differ depending on the type of NAT and the amount of time between one application finishing using the port and another application starting to use the same port that the other application used previously? If so, how?


Solution

  • The public port number belongs to the router, not to App A or App B, and, if it is mapped to a private port number, that mapping also persists beyond any specific application's lifetime.