Java SocketException:没有可用的缓冲区空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6363253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java SocketException: No buffer space available
提问by tulkas85
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 代码使用多个线程,每个线程运行一个 ServerSocket 并进入接受。这些线程使用 java 套接字相互通信。使用 121 个线程都可以正常工作,但是如果我使用 256 个线程运行相同的代码,则会出现此错误:
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.
我使用 windows xp sp3,有几个这样的帖子(这里),但没有人针对此问题发布解决方案。我还安装了一个 Windows 补丁来消除 TCP 连接的限制,但我没有解决我的问题。
采纳答案by jjmontes
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).
确保关闭两侧的所有套接字(在 finally 块中)。请记住,在收到连接后,侦听套接字保持打开状态。不要太快地打开和关闭套接字(我会说它们不能立即重用,这可能与您的问题有关)。
For a better socket-related performance you can use the java.nio API, but it's far more complicated than java.net.
为了获得更好的套接字相关性能,您可以使用 java.nio API,但它比 java.net 复杂得多。
回答by devyndraen
This postI 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?)
这篇文章我想可能有你的答案。留下悬空套接字会导致您达到最大值(这可能由 Windows 补丁未修复的其他一些设置决定(例如 JVM 限制?)
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.
附带说明一下,如果您使用那么多线程运行,很可能最好使用 java.nio 而不是 java 的简单 Socket 和 ServerSocket 类。
This siteis 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.
这个站点是一个很好的教程,介绍了如何使用 java.nio 以及我如何学习它(以及查看Sun/Oracle 的 Java NIO API),来自使用简单的 Socket 和 ServerSocket 编程。
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.
具体来说,使用 Selector 将有助于您的事业,它将允许您以与 C++ select() 调用与套接字相同的方式多路复用套接字 I/O。这意味着在套接字代码的核心,您可以减少由大量线程处理套接字 I/O 的影响,并且只有一个线程直接与套接字交互。
回答by tulkas85
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!
我通过更改 ServerSocket 调用解决了问题。我的错误是将积压值设置得太高。我不需要该服务器侦听超过 5-6 个连接。我替换了: ServerSocket sock=new ServerSocket(port,500); 使用 ServerSocket sock=new ServerSocket(port,10); 现在一切正常!
回答by eze
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.
不知道你在做什么,这只是一个猜测,但我会提供这个以防其他人看到类似的问题。我在尝试打开太多 OUTGOING 连接时看到了这个错误。通过减少缓冲区大小,您可以限制连接线程打开的连接数(当您达到缓冲区限制时,它们将被拒绝)。如果您仍然获得足够的吞吐量,这是一个很好的解决方案。但是,另一种解决方案是增加可用传出连接的数量,这可能会解决您的问题并提供更好的吞吐量。
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)
为此,您可以添加/修改 MaxUserPort 的注册表项。使用 regedit,找到 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters 然后添加/修改键:MaxUserPort 输入 DWORD 和一个高值,如 65534(十进制)
There is a usoft tech articleabout MaxuserPort.
有一篇关于 MaxuserPort的usoft 技术文章。