Establishing multiple TCP connections simultaneously

1.5k views Asked by At

My understanding of TCP in Java is that, while you can multithread handling data transmissions on a socket, you can only establish one connection at a time. Is this true? If not, how would you implement a server that can establish multiple connections simultaneously on the same address and port.

i.e If a client was to try and establish a connection to the server, but is connecting over a very unstable network, would the server have to wait for the connection handshake to complete before being able to accept a connection from another client?

3

There are 3 answers

0
Andy Brown On BEST ANSWER

If a client was to try and establish a connection to the server, but is connecting over a very unstable network, would the server have to wait for the connection handshake to complete before being able to accept a connection from another client?

This aspect of the TCP protocol is addressed by the listen backlog. On linux at least, half-open connections for a server are queued pending completion after which they are ready to accept. The maximum number of half-open connections that a server can handle is capped by the OS. On Linux you can see what the cap is by examining a /proc entry. For example on a RHEL 6 VM:

$ cat /proc/sys/net/core/somaxconn 
128

A malicious entity can exploit this by launching a SYN_FLOOD attack that will fill up the listen backlog and prevent your server from accepting new connections. Mitigating this type of attack (especially when performed by a botnet) is extremely difficult but there are some basic defences as well as some expensive paid services out there.

0
Kayaman On

The server socket isn't forced to wait for a single socket to finish connecting before accepting other connections. While it's waiting for the TCP messages from one endpoint, it can just as easily wait for messages from another.

There was a flaw in TCP earlier that allowed a simple DOS attack by just initiating connections without finishing them, but even that required numerous connections.

0
Dickens A S On

If you refer the JAVA based server implementation thread architecture, most of the server uses a pool and queue based thread. We cannot create N number of thread for each request. But the server maintains a queue of requests with thread pool architecture and process the requests one by one.

Therefore If you are thinking about creating your own JAVA based server. Then plan for creating configurable thread queues and handle the request.

Refer How many concurrent request can tomcat handle by Default