Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

This post I think might have your answer. Leaving dangling sockets would cause you to reach a maximum (which may be determined by some other setting that the windows patch does not fix (such as a JVM limit?)

As a side-note, if you're running with that many threads, it's highly likely that you would be better off using java.nio rather than java's simple Socket and ServerSocket classes.

This site is a very good tutorial for how to use java.nio and also how I learned it (along with looking at Sun/Oracle's Java NIO API), coming from using simple Socket and ServerSocket programming.

Specifically, what will help your cause is using a Selector, which will allow you to multiplex your socket I/O in the same manner that the C++ select() call works with sockets. This means that at the core of your socket code, you can reduce the impact of having a large number of threads dealing with socket I/O and only have one thread interacting directly with the sockets.

I solved problem by change ServerSocket call. My error is to set backlog value too high. I not need that server listen more than 5-6 connection. I replaced : ServerSocket sock=new ServerSocket(port,500); with ServerSocket sock=new ServerSocket(port,10); And now all work at well!

Not knowing what you are specifically doing makes this just a guess, but I'll offer this in case someone else sees a similar problem. I have seen this error when trying to open too many OUTGOING connections. By decreasing your buffer size you have limited the number of connections opened by your connecting threads (they will be refused when you hit your buffer limit). If you still get adequate throughput, that's a fine solution. However, another solution is to increase the number of available outgoing connections, which may fix your problem AND provide better throughput.

To do this, you can add/modify a registry entry for MaxUserPort. Using regedit, find HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters then add/mod key: MaxUserPort to type DWORD and a high value like 65534 (decimal)

There is a usoft tech article about MaxuserPort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top