2

I've been reading about TCP Client/Server communication and I can't find a good description of how multiple Sockets listen to the same port.

I read this,

TCP : two different sockets sharing a port?

and I understand that as long as a Socket can be uniquely identified, then the two Sockets can listen to the same port.

When the server sees a new incoming connection, it branches/forks a new Socket to handle that new connection.

My question is, since the remote client is still sending messages to the same SERVER port (80 in many cases), how does the Server know to which Socket (of the two+ sockets listening to SERVER port 80) the messages should be sent?

My suspicion is that there is some Socket that is a sort of "Dispatcher" that does all the listening dirty work, and distributes incoming messages to the correct Socket.

My suspicion is reinforced by this question,

https://serverfault.com/questions/514704/how-does-server-handle-multiple-clients-on-the-same-port-multiple-sockets-or-mu

but the answer to that question seems really indefinite. Additionally, the top answer here

How does webserver handle multiple connections on the same port

seems to imply that my suspicion is correct, but I don't want to make any assumptions.

Please clear up any misconceptions I have, I only recently started studying networking.

Community
  • 1
  • 1
M. Evers
  • 129
  • 11
  • You should really look at the source: [RFC 793, TRANSMISSION CONTROL PROTOCOL](https://tools.ietf.org/html/rfc793), particularly the parts about multiplexing and connections in [Section 1.5 Operation](https://tools.ietf.org/html/rfc793#section-1.5). – Ron Maupin Sep 26 '16 at 16:51
  • I looked at that section but there wasn't anything there I didn't already know. I understand that a a pair of Sockets uniquely identify a connection. But the remote client is still sending messages to the "fixed sockets which are made known to the public" after a connection is already established. (Unless it's not, which nothing I've read indicates that the client would change the server port to which it was talking) The Server is seeing multiple clients communicate on that port even after the Server creates another Socket to handle the connection. How does the Socket interact with its parent. – M. Evers Sep 26 '16 at 18:08
  • The client is sending with source and destination IP and TCP addresses. That is what forms the unique socket. If the client changed any one of those addresses, e,g. the ephemeral source port, it would be a different socket, but for the same connection, that doesn't happen. What can happen, like with web browsers, is that an application can open several simultaneous connections to get various parts of the web page at the same time. – Ron Maupin Sep 26 '16 at 18:22
  • Perhaps my question isn't clear. I want to know what happens after a connection is established.Client A connects to Server B on Server Port:80. Server B, listening on port 80 with 'Old Socket', creates "New Socket" to handle communications with Client A. Client A, however, is still communicating with Server Port: 80. So messages from Client A are appearing on Port 80 of the Server. What is distinguishing "Old Socket" and "New Socket" so that message that was sent to Server Port 80 arrives at the correct Socket? Are both Sockets listening to Server Port 80 or is one Socket grabbing the message? – M. Evers Sep 26 '16 at 18:46
  • 1
    You are missing the point. I think you are confusing the TCP address (port 80) with the socket and the connection. When the originator or the connection sends with the four addresses which make the socket, that is the socket, and it continues to use that socket even though one of the addresses may be port 80. There are no clients or servers when it comes to TCP. The TCP connection is a connection of peers. The client/server concept is an application concept. – Ron Maupin Sep 26 '16 at 18:52
  • I think I see my confusion. I guess I don't understand what is listening for connections. Is it a Socket that accepts connections from anything (0.0.0.0:xxxx?)? If so, wouldn't that compete for messages with any new Socket on Port 80? – M. Evers Sep 26 '16 at 19:14
  • Let's go back. The socket (the concatenated IP and TCP addresses) can be the same for all the connections, but each connection is unique based on the source socket ( the concatenated source IP and TCP addresses). You can have many connections on the socket, but each connection is unique. As the RFC says: "_A pair of sockets uniquely identifies each connection. That is, a socket may be simultaneously used in multiple connections._" A web server can have many, many different connections based on the source and destination socket combinations, even if there is only one destination socket. – Ron Maupin Sep 26 '16 at 19:25
  • The actual implementation of this is dependent on the OS. – Ron Maupin Sep 26 '16 at 19:25
  • @RonMaupin That makes sense now. I suppose my confusion stemmed from the .Net documentation here [EndAccept](https://msdn.microsoft.com/en-us/library/zdee4kd7%28v=vs.110%29.aspx) which states that a new Socket is created. What they are talking about there is a new Socket Object (which is connected)... not a new Socket as in (IPAddress:Port) pair... – M. Evers Sep 26 '16 at 19:46
  • I think the .Net terminology is confusing. It sounds like it should be a new connection, not a new socket, or maybe it is a new source socket that is meant. – Ron Maupin Sep 26 '16 at 19:56
  • 1
    A *socket* is an endpoint that accesses a *connection*. When a client connects, it creates a *socket` that makes a *connection* to the server. When the server's listening *socket* accepts the *connection*, it creates a new *socket* to handle comms for that *connection*. The *connection* itself is uniquely identified to the OS by the combination of its network type and the IP/port pairs of its two peers. If the server is listening on `192.168.0.1:80`, all of its accepted connections will also be using `192.168.0.1:80` on the server side... – Remy Lebeau Sep 26 '16 at 22:11
  • 1
    What is important is that they all use different IP/port values on the client side. That is what permits multiple clients to be connected to the same server port at the same time. A given client cannot connect multiple times to the same server-side IP/Port using the same client-side IP/Port concurrently. That would be ambiguous. But the client can specify a different IP/Port to connect from. Outgoing clients usually use a random port selected by the OS, however sometimes a client must use a specific IP/port, and that is OK as long as it is unique for that client/server combination. – Remy Lebeau Sep 26 '16 at 22:15

0 Answers0