jQuery in Action, 3rd edition
The moose likes Sockets and Internet Protocols and the fly likes the max number of clients to multithread the socket server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "the max number of clients to multithread the socket server " Watch "the max number of clients to multithread the socket server " New topic

the max number of clients to multithread the socket server

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
see my code bellow,there are three classes,"JavaServer.java" and the "ServerThread.java" are multithread socket server,the "TestClient.java" is the client that simulate the multiple client REPEATEDLY access the socket server:

public class JavaServer {
public static void main(String[] h) {
new Thread() {
public void run() {
try {
ServerSocket ss = new ServerSocket(18000);
boolean listening=true;
Thread t=new ServerThread(ss.accept());
} catch (Exception e) {
System.out.println("the end of main method");

public class ServerThread extends Thread {
Socket socket = null;

public ServerThread(Socket socket) {
this.socket = socket;

public void run() {
try {
System.out.println("Client Accepted");
BufferedReader br = new BufferedReader(new InputStreamReader(

PrintWriter wr = new PrintWriter(new OutputStreamWriter(
socket.getOutputStream()), true);
wr.println("Welcome to Socket Programming");
} catch (IOException e) {

public class TestClient extends Thread {

public static void main(String[] argv) {
try {
for (int i = 0; i < 5000; i++) {
Socket socket = new Socket("localhost", 18000);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(

out.println("My name is Pramod.A");
System.out.println(in.readLine() + i);

} catch (Exception e) {

see the TestClient,it repeatedly connect to the server 5000 time,in the beginning 3000 times it works well (the TestClient can print "Welcome to Socket ProgrammingXXXX"),but when it approach 4000,the following exception is thrown:

java.net.BindException: Address already in use: connect

at java.net.PlainSocketImpl.socketConnect(Native Method)

at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)

at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)

at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)

at java.net.Socket.connect(Socket.java)

at java.net.Socket.connect(Socket.java:402)

at java.net.Socket.<init>(Socket.java:309)

at java.net.Socket.<init>(Socket.java)

at mypackage1.TestClient.main(TestClient.java:16)

if i choose the smaller cycling number such as 2000,it works well,why ?if the socket server has the max connection client limit?

thank you!!
nikos sokaf
Ranch Hand

Joined: May 01, 2004
Posts: 57
I think it depends from your system.
Java is absolutelly a *net* language, so java supports big systems.
Java takes the maximum ports and connection ability of your system and makes the connections.If your system can't have all these, the other connections fails.

This is the way you can destroy a server...

But, keep in mind that there is difficult to handle all these connections.You need code to handle connections that are not used.
Yaroslav Chinskiy
Ranch Hand

Joined: Jan 09, 2001
Posts: 147

The OS has a limited number of sockets. When you creat new TestClient, it gets FD(file descriptor) that locks a socket. So if you do not release it, or make it reusable then after a while TC will throw address is use exception. The TCP/IP has its own timer to release the socket and the FD. The time depends on the OS (around 4-5 min). Java lets you set the timer explicetly. I think this the method is setSoLinger().

It is alwasy better to reuse the same client connection and have some kind buffer that queues the data to be sent and data read from the connection.
I agree. Here's the link: http://aspose.com/file-tools
subject: the max number of clients to multithread the socket server
It's not a secret anymore!