I'm aware of nat table. What happens when two clients in a private local area network want to download exactly the same resource on the same port? What does the router do when the packet comes from the server?

If i'm not wrong, the incoming packet from the server has destination ip address of the router which is public and is the same for both, and also the destination's port number which happens to be the same as well in this case.

How could a router or server detect this or not? or is this behavior even possible at the first place?

I've searched questions like this, which makes sense that the error raises because the port is busy but i'm asking about two separate systems.

Update : From comments I realized that I wasn't clear enough so let me say it again with an example.

I just care about the source port of the devices Assume I have two laptops ( and ), both of them are downloading same file from the same server somewhere in the internet. Each of them has an operating system which generates a random port so the source IP and source port would be something like: and . I thought that in NAT, router will set it's (public)IP address along with the ports from laptops so if the public IP address of the home router is , these two packages will get these source IP and source port respectively : and .

The router will know which port the packets are for which laptop so far so good. What happens when two laptops create exactly the same port of source? for example : and . Now router will set it's public IP address as the source IP address just as before, but now if it tries to use those port numbers, those packages will have exactly the same source IP and source port number, like : and .

When a response comes back how does the router know which packet is for which laptop?

After @mfinni's answer, i noticed that this is not how pat works! The NAT device (here router) will assign unique ports to each individual laptop(private IP address), then the packets sent out with these unique ports(for example 7777 and 7778 ). So when response gets back, it's clear that which packet is for which laptop from the ports, then router will convert these , to --> , respectively.

Best Answer

The tcp connection underpinning http and many other protocols is at a given point in time defined by 4 parameters

  • The local IP
  • The local port
  • The remote IP
  • The remote port

Even if you send the same request twice simultaneously from the same computer even with the two ip addresses identical and the destination port identical the source port will be different

The nat device that is connected to nat can use different ports Depending on the device, it may either keep the original source ports (and only change one if there's a conflict), or always assign a new source port independently of the original source port.

The nat device will then keep for each connection a mapping in its translation table which states that external connection (external ip, external source port, destination ip, destination port) is mapped to internal connection (internal host ip, internal host source port, destination ip, destination port).