Skip to main content

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

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?


Question Credit: Stephen
Question Reference
Asked March 15, 2019
Posted Under: Network
13 views
2 Answers

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.


credit: Michael Hampton
Answered March 15, 2019

Read up on TCP - that's what creates and tracks sessions. The client will be identified by its IP and sending port, so that's at a minimum. For some protocols/applications, there's further refinement like cookies, keys, etc.

https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Resource_usage

Most implementations allocate an entry in a table that maps a session to a running operating system process. Because TCP packets do not include a session identifier, both endpoints identify the session using the client's address and port. Whenever a packet is received, the TCP implementation must perform a lookup on this table to find the destination process. Each entry in the table is known as a Transmission Control Block or TCB. It contains information about the endpoints (IP and port), status of the connection, running data about the packets that are being exchanged and buffers for sending and receiving data.


credit: mfinni
Answered March 15, 2019
Your Answer