Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a tcp connection, how possibly can a server handle more than 65535 client at an instant?

I've been reading this socket tutorial by Oracle and stumbled upon the following text:

If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.

Now if I'm not wrong then the port size is 16 bit which limits the max no of ports around 65K. This means that a server can't handle more than 65535 connections at any instant if all of it's port are bound to some client local port. While some answers like this on stackoverflow suggest that there's no limit on active connections. What is true about this and what is wrong?

Edit 1: If indeed a server can't handle more than 2^16-1 connections, then how do websites like Google handle this limitation?

like image 581
आनंद Avatar asked Jun 17 '17 14:06

आनंद


People also ask

How many clients can a TCP server handle?

On the TCP level the tuple (source ip, source port, destination ip, destination port) must be unique for each simultaneous connection. That means a single client cannot open more than 65535 simultaneous connections to a single server. But a server can (theoretically) serve 65535 simultaneous connections per client.

Can TCP handle multiple clients?

The server can receive any number of connections on its single listening port, as long as a different address/port combination is used by each client.

Is there a limit on TCP connections?

Ports are 16-bit numbers, therefore the maximum number of connections any given client can have to any given host port is 64K.

How does a server handle multiple clients?

The simple way to handle multiple clients would be to spawn new thread for every new client connected to the server.


1 Answers

A unique TCP connection is defined by a unique combination of client IP, client port, server IP and server port. For a specific service server IP and port are constant (i.e. port 80 for HTTP), but client IP and port can vary. Since the port range is only 1..65535 this means that the server can only handle at most 65535 different connections from the same client IP address at the same time, because these are all possible unique combinations of the connection tuple when only the port can be changed. But, if there are multiple clients with different IP addresses this limitations applies to each of these clients separately. If you then look at the amount of different possible IP addresses (IPv4 and IPv6) you'll see that there is essentially no real limit of how much connections the server could handle in theory.

In practice each of these TCP connections takes memory at the server since the current state has to be kept. Additional memory is needed in kernel and application for file descriptor and application protocol state etc. This means that there is a practical limit based on the resources of the machine which might be less then 64k but also way more, depending on the system and its configuration.

like image 56
Steffen Ullrich Avatar answered Oct 01 '22 02:10

Steffen Ullrich