Java SocketException: No buffer space available

tulkas85 picture tulkas85 · Jun 15, 2011 · Viewed 23.3k times · Source

My java code use several threads, each thread run a ServerSocket and goes in accept. These threads communicate each other using java socket. With 121 threads all work at well, but if I run same code with 256 thread I have this error:

java.net.SocketException: No buffer space available (maximum connections reached?): listen failed
    at java.net.PlainSocketImpl.socketListen(Native Method)
    at java.net.PlainSocketImpl.listen(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)

I use windows xp sp3, there are several post like this (here ), but nobody post a soution for this problem . I have also installed a windows patch for remove the limit on TCP connection but I not solved my problem.

Answer

jjmontes picture jjmontes · Jun 15, 2011

The message says you may be running out of connections. Have you checked that? You can check the open sockets from the command line using:

netstat -n

Ensure that you are closing all sockets on both sides (in finally blocks). Remember that listening sockets remain open after you receive a connection. Don't open and close sockets too quickly (I'd say they can't be reused immediately which could be related to your issue).

For a better socket-related performance you can use the java.nio API, but it's far more complicated than java.net.