Socket Getting Created with Same Ip and Port on Local Host

Socket bind port to same port in my localhost and box IP

Each of those is almost certainly a different interface and hence a different internet address. That is, 127.0.0.1 is typically the loopback interface. While presumably xx.xx.xx.xx is a real (ethernet) network interface. It is entirely possible to have two separate programs bound to the same port number on separate addresses. It is more common that a single program simply binds to the port number and the kernel in effect translates that into multiple binds, one for each interface's address.

See bind(2) and ip(7) manual pages for details. Specifically, INADDR_ANY is the pseudo-address that can be used by a server that wishes to bind the port on all interfaces.

See also the answer here under the first paragraph of the Linux subheading:

Socket options SO_REUSEADDR and SO_REUSEPORT, how do they differ? Do they mean the same across all major operating systems?

Can two applications listen to the same port?

The answer differs depending on what OS is being considered. In general though:

For TCP, no. You can only have one application listening on the same port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.

For UDP (Multicasts), multiple applications can subscribe to the same port.

Edit: Since Linux Kernel 3.9 and later, support for multiple applications listening to the same port was added using the SO_REUSEPORT option. More information is available at this lwn.net article.

Connected to the same router = Same ip and port?

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 |
--------


Related Topics



Leave a reply



Submit