This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
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
Author

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);
ss.setReceiveBufferSize(900000);
boolean listening=true;
while(listening){
Thread t=new ServerThread(ss.accept());
t.start();
t.join();
}
ss.close();
} catch (Exception e) {
System.out.println(e);
}
}
}.start();
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(
socket.getInputStream()));
System.out.println(br.readLine());

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








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(
socket.getInputStream()));

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

out.close();
in.close();
socket.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}




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
Hi,

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