I have a router with three computers connected to it via Wi-Fi. One day, computer A requests the file "index.php" from a server with IP address XYZ. Then the second computer, B, requests the file "home.html" from the same server, and computer C requests "picture.jpg" from the same server. If all these requests are made at the exact same time, how does the router know which file to send to which computer? Could these simultaneous requests cause problems such as sending "home.html" to computer C instead of B?
closed as not a real question by CharlieRB, BBlake, Dave M, Simon Sheehan, Keltari Jan 28 '13 at 15:46
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. If this question can be reworded to fit the rules in the help center, please edit the question.
Sender's IP address is stored in its messages, so that the receiver can send back a response to the appropriate computer. In that case, the router just routes packets.
When the computers on a private LAN behind a single IP address, the router also performs Network Address Translation. In that case, the router maintains a translation table for routing the packets to the appropriate computer.
Whenever a client visits a website, it attempts to open a TCP connection between itself and the server. This connection is uniquely identified by its destination IP address, destination port, source IP and source port. For a client behind IP 18.104.22.168 visiting superuser.com, the connection could appear as:
Web servers generally run on port 80, the default port for HTTP traffic, so clients would send their requests to view a web page to that port. If the server would use the same port to send its reply, browsing the web would interfere with the operation of a web server the client might be running himself. Fortunately, the client initiating the connection can use whatever source port is available. This port will be reserved as temporarily in use until both machines agree on closing the connection. The IANA suggests to use the range 49152 to 65535 for these so-called 'ephemeral ports', a recommendation most operating systems adhere to nowadays.
Below is a screenshot of a server showing one SSH and two HTTP connections, as well as the ephemeral ports the machine is responding to.
While the operating system ensures no connections are opened on source ports already in use, it cannot reserve them for the entire local area network. However, the router can. Though not technically required, in the most common NAT setup, the router rewrites both the source address and port of all outgoing packets, hence 'network address (port) translation', maintaining a table of all open connections. Some routers even allow you to view this table directly, for instance using telnet:
By reassigning port numbers, the router can keep track of every connection individually and forward replies to the appropriate host. For example, this is what happens when machines A and B simultaneously try to access superuser.com from the same network, accidentally using the same ephemeral port.
In conclusion, as long as your NAT is working correctly, simultaneous requests from different computers on your network that risk being mixed up are rewritten by the router to be easily distinguishable, making sure the right web page ends up at the right browser.