views:

164

answers:

4

Hi,
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

+3  A: 

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.

zigdon
A: 

The operating-system on which the server runs uses the remote port number to distinguish between the various connections to the server.

Steve Emmerson
+3  A: 

It serves multiple clients and you can choose how many clients you will handle a the same time.

A connection (aka a Socket between a client and a server isn't only identified by the ServerIP/ServerPort, it's identified with ClientIP/ClientPort/ServerIP/ServerPort.

You only have to accept connections (and usually treat them in different threads).


By default the backlog size is 50, but you can set it when you create your ServerSocket.

new ServerSocket(21, 100); //Create a server socket with a backlog of 100

Resources :

Colin Hebert
+1  A: 

See e.g. this answer on Stackoverflow.

Andre Holzner