How does serversocket class serve multiple client connections on same port?

keshav84 picture keshav84 · Sep 16, 2010 · Viewed 13.1k times · Source

When using a Socket class one is establishing a TCP connection to a server on some port, but on the server the ServerSocket is capable of handling multiple client connections for each accept request and delegate it to a thread to server the request. But how is it possible for a ServerSocket class to accept multiple tcp connections on the same port.

Does it mean that it is upto the OS to decide how many connections it allows or what is the maximum backlog allowed and can this be controlled by applications on top of OS(i mean is java restricted by the maximum backlog supported by OS) and is there any privison for backlog connections in TCP specification?

Best reagards,
Keshav

Answer

zigdon picture zigdon · Sep 16, 2010

A TCP connection is defined by a unique set of (source IP, source port, dest IP, dest port). Since the server binds to a particular port, it defines two of those 4 variables. As long as the clients all come from different IPs and/or different ports, it won't be an issue.

And yes, the OS can control how many total connections are allowed, and your program can restrict that even further.