views:

20

answers:

1

hi, i am working upon serversocket class and opening threads when a new client hits the serversocket....

serverSocket = new ServerSocket(port);

while (true) {
        Socket clientSocket = serverSocket.accept();
        new Thread(this).start();//opening multiple threads
}

but when 5000 clients hits on this serversocket an error comes of java.net.ConnectException: Connection refused: connect at many of the clients side. so plz tell me what is the better way to open a serversocket so that it can accept all the client connections? there is a 1 millisecond time gap between simultaneous client connections.

can anybody suggest me? thanks in advance..

+1  A: 

There are 2 things you should be checking out definately.

File descriptor limit. On a Linux based system, you can check that using ulimit.

Secondly, Serversockets have waiting queue size, if you have more connections waiting than queue size, they will be refused immediately. You should try ServerSocket(port, backlog) constructor.

Rohit
In addition to this, check your RAM. 5000 threads, each will by default use 256k(depending on OS etc. might be 512k) for its stack. That adds up, you might run out of virtual memory on a 32 bit system.
nos