Java sockets: no free space in the buffer (maximum connections reached?)

I have a big problem. I developed a client-server application. The client thread sends the serialized object to the server, and the server sends back the serialized object. I currently use one server and 10 client threads, and after about 30 seconds I get an error message from each client thread (IOException):

No free space in buffer (maximum number of connections reached?): Connect

If I look in netstat, I see that there are many connections that grow and grow, and all connections are in TIME_WAIT state.

I do not know why. I close sockets on the server and in the clients every time in the finally block. Here is the code:

On the server, I have socketHandlerThread:

 ServerSocket serverSocket = new ServerSocket(port); serverSocket.setSoTimeout(5000); while(true) { Socket socket = serverSocket.accept(); } 

Then the new socket is placed in the LinkedBlockingQueue, and the worker thread accepts the socket and does the following:

 try { outputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream())); outputStream.flush(); inStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); ClientRequest clientRequest = (ClientRequest) inStream.readObject(); ... outputStream.writeObject(serverResponse); outputStream.flush(); } catch.... } finally { if (inStream != null) { inStream.close(); } if (outputStream != null) { outputStream.close(); } if (socket != null) { socket.close(); } } 

On the client side, I have the following code:

  try { socket = new Socket(host, port); outputStream = new ObjectOutputStream(new BufferedOutputStream(socket.getOutputStream())); outputStream.flush(); inputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream())); outputStream.writeObject(request); outputStream.flush(); Object serverResponse = inputStream.readObject(); } catch.... } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } if (socket != null) { socket.close(); } } 

Can anyone help? I really don't know what mistake I made. Sockets don't seem to close, but I don't know why.

Maybe the problem is that I queued the sockets on the server side so that the socket was copied somehow?

Edit: if I put the client and server on a different instance of Amazon EC2 classic running Linux AMI, then it works. Could this be a problem with Windows or the problem is that I was running clients and servers on the same computer (my local computer)?

Does anyone see an error in my code?

Edit2: As stated above in EC2 instances, this works, but if I use netstat, it shows many more lines saying TIME_WAIT .

Here are the screenshots:

https://drive.google.com/file/d/0BzERdJrwWrNCWjhReGhpR2FBMUU/view?usp=sharing

https://drive.google.com/file/d/0BzERdJrwWrNCOG1TWGo5YmxlaTg/view?usp=sharing

The first screenshot from the windows. "WARTEND" means "WAITING" (this is German).

The second screenshot is from Amazon EC2 (to the left of the client machine, to the right of the server machine).

+5
source share
1 answer

TIME-WAIT is entered after closing the connection at both ends. This takes a couple of minutes, for data integrity purposes.

If the buffer problem is related to TIME-WAIT states on the server, the solution should make the server equal to the one that receives the closure first. This will change the TIME-WAIT state to the client, where it will be benign.

You can do this by putting server-side request processing in a loop so that it can process several connection requests and so that the server closes only that socket when it reaches the end of the stream on it.

 for (;;) { try { ClientRequest clientRequest = (ClientRequest) inStream.readObject(); ... outputStream.writeObject(serverResponse); outputStream.flush(); } catch (EOFException exc) { break; } } 

If you then implement a connection pool on the client, you will significantly reduce the number of connections, which will further reduce the frequency of the buffer problem.

+3
source

Source: https://habr.com/ru/post/1204851/


All Articles