12

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

keshav84
  • 2,291
  • 5
  • 25
  • 34
  • See e.g. [this answer](http://stackoverflow.com/questions/3638953/do-tcp-connections-get-moved-to-another-port-after-they-are-opened/3639017#3639017) on Stackoverflow. – Andre Holzner Sep 16 '10 at 19:08

3 Answers3

8

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
  • 14,573
  • 6
  • 35
  • 54
  • Does it mean that we could actually have *unlimited* connections and not just 65k? – Pacerier Feb 19 '12 at 21:02
  • Not unlimited, but running out of IP/sport/dport combos will not be the limiting factor. – zigdon Feb 20 '12 at 18:15
  • Is it true that a computer trying to connect to itself can have a theoretical limit of 65536*65536 = 4294967296 connections to itself? Or did I mess up my maths.. – Pacerier Feb 20 '12 at 18:43
  • Sounds about right - except that keep in mind you can set up multiple IP addresses to the same computer. – zigdon Feb 21 '12 at 02:37
3

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
  • 91,525
  • 15
  • 160
  • 151
  • I don't think that you can control the `backlog`, you can specify it but cannot control it, if you think you can and 2nd argument of constructor works then you can want to have a look at this question of mine - http://stackoverflow.com/questions/41309474/having-issue-with-testing-backlog-as-per-serversocketint-port-int-backlog – hagrawal7777 Dec 25 '16 at 12:20
-1

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

Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59