1

TCP connection is defined by [client IP address:outgoing port - server IP address:incoming port]. Web server listens on one port but handles multiple tcp connections on the same port - how is that achieved? Is one connection somehow transferred to other incoming port to be able to listen to new connection?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

2

Even if the server IP and server port are the same, the client IP or client port are different. So there is no problem, from the TCP point of view.

If you want more details, in the server code there is a socket destined to accept connections, that will be the listening socket and it will be bound to the server port (and optionally to the IP). When a client connects to this port the listening socket accepts it and a new connected socket is returned.

Even if all the server sockets use the same port, only one of them is actually listening, so there is no problem.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • thanks, can you please elaborate on this _Even if all the server sockets use the same port, only one of them is actually listening, so there is no problem._? – Max Koretskyi Nov 14 '15 at 05:42
  • 1
    @Maximus: Sure. A socket is an OS abstraction to a TCP connection. The client just creates the socket and connects it. However, the server creates a listening socket, bound to the port. This socket does not represent an actual connection, but the endpoint to accept connections. When a client requests arrives to a listening socket, it splits out a connected socket. If you look carefully you never have two sockets with the same _peer data_ (IPs + ports). – rodrigo Nov 14 '15 at 10:09
  • thanks, where can I read more about `it splits out a connected socket`? I'm interested in top level socket programming concepts – Max Koretskyi Nov 15 '15 at 08:16
  • 1
    @Maximus There are plenty of examples and tutorials in Internet, in whatever language you wish. Just google _TCP server example node.js_. (The function you ask about is `accept`). – rodrigo Nov 15 '15 at 09:19
  • sure, thanks, I'll try to find something good – Max Koretskyi Nov 15 '15 at 09:40