A TCP connection is identified by its four-tuple. These are four values:
- source IP
- source port
- destination IP
- destination port
If you add the protocol, it becomes a five-tuple. A port alone doesn't identify a connection.
The server has one listening socket. A socket is the OS object a program uses to send and receive network data. The listening socket is bound to an address and port, for example 0.0.0.0:443. Each call to accept() returns a new connected socket for one client. All these sockets share local port 443. But each one has a different remote IP and port, so every four-tuple is unique. The real limits are memory and file descriptors, not ports.
The bind address matters:
127.0.0.1accepts connections only from the same machine.0.0.0.0(or::for IPv6) accepts connections on every network interface.
Clients use a temporary ephemeral port for their end of the connection. This is where port limits really cause problems.
- A connection is identified by source IP, source port, destination IP and destination port.
- The server has one listening socket, and accept() creates a new connected socket for each client.
- All connections can share the same server port.
- Binding to 127.0.0.1 accepts local connections only, and 0.0.0.0 accepts them on all interfaces.
- Clients use temporary ephemeral ports for their end.
Read more