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

3

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

Platt answered 16/9, 2010 at 18:52 Comment(1)
See e.g. this answer on Stackoverflow.Atilt
H
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.

Hue answered 16/9, 2010 at 18:58 Comment(4)
Does it mean that we could actually have unlimited connections and not just 65k?Bosanquet
Not unlimited, but running out of IP/sport/dport combos will not be the limiting factor.Hue
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..Bosanquet
Sounds about right - except that keep in mind you can set up multiple IP addresses to the same computer.Hue
J
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 :

Jungian answered 16/9, 2010 at 18:59 Comment(1)
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 - #41309974Chamblee
C
-1

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

Corpora answered 16/9, 2010 at 18:58 Comment(1)
It uses the 4-tuple.Gesticulation

© 2022 - 2024 — McMap. All rights reserved.