java.net.SocketException: No buffer space available (maximum connections reached?): connect

java.net.SocketException: No buffer space available (maximum connections reached?): connect

A java application deployed in our infrastructure was failing to start and giving this error on a regular basis which was strange as there was no limitation on memory and perm assignment. Everything seems perfect but still it was not able to start and function as expected.

Another exception was written in the log which was strange as well:

java.sql.SQLException: Network error IOException: Address already in use: connect

Both errors have different meanings and different solution but in this case there was no problem with the application. There was no bug or code issue. The issue was with the windows server on which this application was hosted.

This is due to the windows having too many active client connections. The default number of ephemeral TCP ports is 5000. It may happen this number reaches the limit which cause this application to crash and provide different reasons while startup. Sometimes this number may be insufficient if the server has too many active client connections. In that case the ephemeral TCP ports are all used up and no more ports can be allocated to a new client connection request which results in the error message “java.net.SocketException: No buffer space available (maximum connections reached?): connect” or “java.sql.SQLException: Network error IOException: Address already in use: connect”.

Environment

I am using Windows Server 2016 and I hope this solution will work for other versions as well or maybe its already solved in Microsoft Windows Server 2019 Server. Anyway, please follow the under listed instructions to solve this problem. In my case, there was no need to restart the server but please restart the server if required.

Solution!!!!

The resolution is to open the registry editor and locate the registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters and add a new entry :
Value Name: MaxUserPort
Value Type: DWORD
Value data: 65534