Java Sockets: No buffer space available (maximum connections reached?) Java Sockets: No buffer space available (maximum connections reached?) multithreading multithreading

Java Sockets: No buffer space available (maximum connections reached?)


TIME-WAIT is entered after the connection is closed at both ends. It lasts for a couple of minutes, for data integrity reasons.

If the buffer problem is due to TIME-WAIT states at the server, the solution is to make the server be the peer that first receives the close. That will shift the TIME-WAIT state to the client, where it is benign.

You can do that by putting your server-side request handling into a loop, so that it can handle multiple requests per connection, and so that the server only closes the socket when it reaches end of stream on it.

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

If you then implement connection-pooling at the client, you will massively reduce the number of connections, which will further reduce the incidence of the buffer problem.