nikos
nikos

Reputation: 3003

Multithread server program in Java

I am trying to write a multithread program in Java where a server listens for connections from clients and spawns a thread to acommodate each client. I have:

while(true)
    {
        Socket s = server.accept();    
        ClientHandler ch = new ClientHandler(s);
        Thread t = new Thread(ch);
        t.start();  
    }

My question is: whenever it accepts a connection in

Socket s = server.accept();

and starts executing the following lines of code to create the thread etc., what happens to a request for connection from a client during that time. Is it queued somehow and it will get served in the next loop of while(true) or will it be rejected?

thanks, Nikos

Upvotes: 5

Views: 1550

Answers (4)

cybye
cybye

Reputation: 1171

ugh :)

there is a so called SYN-queue, which takes up to the requested amount of not yet established connections (there is a per call and a system-wide limit - not sure if its even user limited).

when "listening" on a ServerSocket, you specify that by - one of the answers says "Nusers" but it is - the size of the "backlog" the socket should keep. in this backlog the pending connections are stored and if it is filled up, all other (should) get a ConnectionRefused.

So

a) increase that when you are to slowly accepting (server.accept()) connections

b) accept the connections faster by

b.1) using a ThreadPool (be happy moving the problem to context-switches of the os)

b.2) use NIO and with that the ability to handle socket-states within single threads/CPUs (as long as the internal data throughput is better than the one in the network, this is the more performant option)

have fun

Upvotes: 0

Xiaowei Chen
Xiaowei Chen

Reputation: 11

I wrote a tiny http server in Java, which you can find on github. That might be a good example for you to take a look at real-world usage of Sockets and multithreading.

Upvotes: 1

quinestor
quinestor

Reputation: 1442

As it was answered already: yes it is queued and no it is not rejected.

I think that anyone reading this question should first know that when you instantiate a socket:

server = new ServerSocket(mPort, mNusers);

Java is already implementing a network socket ; which has clearly defined behavior. The connection will be rejected however after reaching the limit set.

Also, the code posted in the question accepts multiple connections but looses the refference for the previous. This may be a "duh" but just in case someone is copy-pasting , you should do something to store all the created sockets or handlers. Perhaps:

ClientHandler[] ch = new ClientHandler[mNusers];
int chIndex = 0;
while(true)
    {
        Socket s = server.accept();    
        ch[chIndex] = new ClientHandler(s);
        Thread t = new Thread(ch);
        t.start(); 
        chIndex++;
    }

An array may not be the best option but I want to point out that with sockets you should know what's the limit of connections you will allocate

Upvotes: 0

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84149

After the accept() returns the TCP handshake is complete and you have a connected client socket (s in your code). Until the next call to accept() the OS queues pending connection requests.

You might want to check out some tutorial like this one for example.

Upvotes: 4

Related Questions