Reputation: 135
Three computers connected to the same router will have the same public IP. If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?
EDIT: When I get requests from the browser, the PORT is different for each connection it creates. Does the browser client just pick a random port that is available on the router?
Upvotes: 0
Views: 196
Reputation: 597860
Three computers connected to the same router will have the same public IP.
Correct, from the server's perspective, not from the client's perspective.
If these computers send a request to my server, will they all have the same PORT as well or are there exceptions?
No, they will not have the same Port on the router (they may on each client PC, though).
A TCP connection is uniquely identified by the tuple {protocol, local-ip, local-port, remote-ip, remote-port}
. So, when multiple TCP connections have the same {remote-ip, remote-port}
(IOW, when multiple clients are connected to the same server), then each {local-ip, local-port}
must be unique. And vice versa, when multiple TCP connections have the same {local-ip, local-port}
(IOW, when a client connects to multiple servers), then each {remote-ip, remote-port}
must be unique.
When passing through a router, each TCP connection as seen on the client side will be {TCP, lan-ip, lan-port, server-ip, server-port}
, while on the server side each connection will be seen as {TCP, listen-ip, listen-port, client-ip, client-port}
, where {client-ip, client-port}
will be the router's {public-ip, public-port}
, so each {public-ip, public-port}
must be unique.
So, multiple clients connecting to the same server through a router simply cannot use the same outgoing port on the router, otherwise the server would not be able to differentiate between the connections.
When I get requests from the browser, the PORT is different for each connection it creates.
Correct.
Does the browser client just pick a random port that is available on the router?
No, nor does the browser care that a router is present. The browser creates a local socket endpoint and binds it to an available {local-ip, local-port}
and then uses it to connect to the server's {server-ip, server-port}
. The packets go to the OS, the OS sends them to the router, the router opens its own available {public-ip, public-port}
for each new connection and then forwards those packets to the server. When the server sends packets back, the router will receive those packets on its public NIC, forward them to the appropriate client OS, which will pass them to the appropriate socket endpoint.
------------- | Client PC A | ------------- {tcp, client-lan-ip, client-lan-port, server-ip, server-port} /|\ | \|/ {tcp, router-lan-ip, router-lan-port, client-lan-ip, client-lan-port} -------- | Router | -------- {tcp, router-public-ip, router-public-port, server-ip, server-port} /|\ | \|/ {tcp, listen-ip, listen-port, router-public-ip, router-public-port} -------- | Server | --------
Upvotes: 1