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

2

7

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?

Calvaria answered 17/6, 2017 at 14:31 Comment(0)
E
10

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 0...65535 this means that the server can only handle at most 65536 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.

Endamage answered 17/6, 2017 at 15:38 Comment(0)
T
-2

They use something like NAT (network address translation) for your ISP. You can access different computer behind your router because your router maps the routes to the PCs internally.

E.g. Google data center does the same thing. Mapping "Google.com" to different internal server allowing them to accept more than 65k connections in total.

Twofaced answered 17/6, 2017 at 15:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.