how does an OS route messages sent to the same port to different sockets?

Stephen asked:

This is something I’ve never quite understood: A service offered by a server can be uniquely identified by its IP address and port, for example 1.2.3.4:22, but multiple clients might connect to the same port at the same time. When the different clients send new data to the server, how does the server distinguish between them so that it can route the data differently?

For example, when a server allows SSH connections, it will open port 22 by default. Multiple SSH clients can connect so that multiple people can be using SSH at the same time, and they will all be using port 22. I believe that on the server, the different sessions are distinguished by having different sockets, one for each session, though I could be wrong about that.

But if that is the case, when a particular user enters a new command during their SSH session, how does the server know which socket to route that command to? The user’s computer doesn’t have access to the socket descriptor, they only have the IP address and port, so I don’t see how they could send enough information to the server to allow it to distinguish between their session and another session. Does the server distinguish based on the IP address and port of the client, or does it do something else?

My answer:


Each end of the connection has its own IP address and port. The “client” (it’s not a client as TCP/IP is peer to peer; it’s the initiator) has the source IP and source port on its own system, to which packets are addressed. The connection is defined by all four of the source IP, source port, destination IP and destination port, which makes it trivial to disambiguate connections.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.