3

I was wondering, how multiple applications can use the same network port. AFAIK in TCP protocol 1 port is assigned to 1 socket connection. So how, for example, more than one internet browser can use ports 80/8080 at the same time? Can I bind more than one socket to the same port? How can I do that in C++?

wojciechowskip
  • 103
  • 1
  • 11
  • The standard thread for this topic is at http://stackoverflow.com/q/14388706/632951 – Pacerier Sep 18 '15 at 09:53
  • @Pacerier Not really. That's pretty tangential to this question. I would cite [this one](http://stackoverflow.com/questions/1575453/how-many-socket-connections-can-a-web-server-handle?rq=1). – user207421 Feb 27 '16 at 19:01

3 Answers3

6

A socket connection is uniquely identified by a combination of its local IP:Port and remote IP:Port. Multiple apps can be bound to the same local IP:Port as long as they are each connected to a different remote IP:Port.

If a local IP:Port is already bound for listening (bind() and listen() have been called for it), other sockets can still bind() to that same local IP:Port but only if the SO_REUSEADDR (and on some platforms, SO_REUSEPORT) socket option is used. Otherwise, the bind() fails with an "already in use" error.

When multiple client sockets connect() to the same remote IP:Port, a local binding is typically not specified, which allows connect() to perform an implicit bind() to a random available local IP:Port to avoid conflicts with other connections. If bind() is explicitly called and succeeds, and then connect() is called to connect to a remote IP:Port that is already connected to the local IP:Port, connect() will fail.

Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • +1, the unique identification by the socket pair is the key thing. A server servicing multiple clients on the same IP & port needs to look at the IP & port of the client on any information it receives in order to know which connected socket it should be sent to. – Crowman Apr 29 '14 at 00:13
  • Your answer is what I was looking for. Thanks for explanation. – wojciechowskip Apr 29 '14 at 00:17
  • @PaulGriffiths That's pretty misleading. *TCP* needs to do that, not the server. All the server has to do is reply via the same socket the request came from. – user207421 Nov 08 '14 at 22:14
  • @EJP: That's pretty pointless and obfuscatory quibbling. You might as well say that a computer doesn't send data across the network at all, since that's all done by wires and radio waves. And that's before we ever even get to the fact that a "server" can refer to an entire computer, not just to an application running on it, in which case your comment would be just flat-out wrong, and not simply "misleading". – Crowman Nov 08 '14 at 23:46
1

A TCP port can only have a single socket listening for connections. When a connection is made via accept() or friends, a new socket is generated, that represents this connection, while the single original listening socket keeps listening.

Eugen Rieck
  • 60,102
  • 9
  • 66
  • 89
  • Summing up, in relation with @user3581454, at server side socket is listening all the time for connections on port 80 and it allows to handle connections simultaneously. At client side every single instance of socket need to have different port assigned, as user3581454 showed in his example. Am I correct? – wojciechowskip Apr 29 '14 at 00:13
  • The defining tuple for a tcp packet is (source-ip, source-port, destination-ip, destination-port). Since the destination part is fixed for a connected socket, (source-ip, source-port) must be unique for a connection. Typically the client's OS assures this. – Eugen Rieck Apr 29 '14 at 00:15
  • You could, in principle, have multiple outbound connections on the same IP/port at the client side as long as they're all going to different IP/port combinations at the server side. But this doesn't generally happen, since you typically don't care what the client port is, since the client is trying to make contact with someone else. – Crowman Apr 29 '14 at 00:16
  • From the server's POV, once a connection is made, the destination parameters are fixed. So the source parameters define the connection. – Eugen Rieck Apr 29 '14 at 00:17
0

In TCP, which is a stateful protocol, a connection is defined uniquely by the tuple [source_ip, source_port, dest_ip, dest_port] (look at Eugen Rieck's comment above). So theoretically, each client(or set of clients behind a NAT) could connect to the server on any 16-bit port number (minus typically the ports from 0-1023).

When a web server is listening on port 80(for example) for incoming HTTP connections, each time the client tries to send an HTTP request to the server, the client initiates a TCP connection over a different client port. So the answer to how multiple applications can use the same network port is by having a different port on the other side.

forumulator
  • 678
  • 8
  • 12