Reputation: 4310
My TCP Server
public static void main(String argv[]) throws Exception {
ServerSocket server = new ServerSocket(3333);
System.out.println("Server started");
while (true) {
Socket socket = server.accept();
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
System.out.println("Received: " + input.readLine() + "\n");
}
}
My TCP Client.
public static void main(String[] args) throws IOException {
for (int i = 1; i < 50000; i++) {
Socket clientSocket = new Socket("localhost", 3333);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
outToServer.writeBytes(i + "\n");
clientSocket.close();
System.out.println(i);
}
}
The loop in TCP client runs till 16374 and stopped, throwing this exception.
...
...
...
16372
16373
16374
Exception in thread "main" java.net.SocketException: No buffer space available (maximum connections reached?): connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at mawia.test.Client.main(Test.java:11)
Upvotes: 1
Views: 796
Reputation: 1539
Also try once with adding flush()
in your TCP Client code before closing the connection, worked with me.
Upvotes: 0
Reputation: 7692
Fildor is right, you are creating too many connections or Socket handles
, this internally translates to allowed open-file-handles
per process which in most probability you are breaking.
Ideal approach would be for client to keep on sending data (50000 records
) on one connections i.e. Socket
and Server
should handle processing this data in a separate thread (So you can run multiple TCP Client concurrently):
TCP Server
public static void main(String argv[]) throws Exception {
ServerSocket server = new ServerSocket(3333);
System.out.println("Server started");
while (true) {
final Socket socket = server.accept();
Thread t = new Thread(new Runnable() {
public void run() {
System.out.println("Server received message from client: ");
BufferedReader input;
try {
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(!socket.isClosed() && socket.getInputStream().available() > 0){
System.out.println("Received: " + input.readLine() + "\n");
}
System.out.println("Client disconnected from server");
} catch (IOException e) {
//break;
}
}
});
t.start();
}
}
TCP Client
public static void main(String[] args) throws IOException {
Socket clientSocket = new Socket("localhost", 3333);
for (int i = 1; i < 50000; i++) {
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
outToServer.writeBytes(i + "\n");
outToServer.flush();
System.out.println(i);
}
clientSocket.close();
}
Upvotes: 2