• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

the max number of clients to multithread the socket server

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic