简体   繁体   English

Java SocketException:没有可用的缓冲区空间

[英]Java SocketException: No buffer space available

My java code use several threads, each thread run a ServerSocket and goes in accept. 我的java代码使用多个线程,每个线程运行一个ServerSocket并进入接受。 These threads communicate each other using java socket. 这些线程使用java套接字相互通信。 With 121 threads all work at well, but if I run same code with 256 thread I have this error: 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 . 我使用windows xp sp3,有几个这样的帖子( 这里 ),但是没有人为这个问题发帖。 I have also installed a windows patch for remove the limit on TCP connection but I not solved my problem. 我还安装了一个Windows补丁,以消除TCP连接的限制,但我没有解决我的问题。

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. 为了获得更好的与套接字相关的性能,您可以使用java.nio API,但它比java.net复杂得多。

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?) 离开悬空套接字会导致达到最大值(这可能是由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 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. 这个站点是一个非常好的教程,如何使用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. 具体来说,有助于您的原因是使用Selector,它允许您以与C ++ select()调用与套接字一起使用的方式多路复用套接字I / O. 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 / O的影响,并且只有一个线程直接与套接字交互。

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. 尝试打开太多OUTGOING连接时,我看到了这个错误。 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. 为此,您可以添加/修改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) 使用regedit,找到HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ services \\ Tcpip \\ Parameters然后添加/ mod键:MaxUserPort键入DWORD,高值如65534(十进制)

There is a usoft tech article about MaxuserPort. 一篇关于MaxuserPort的软件科技文章

I solved problem by change ServerSocket call. 我通过更改ServerSocket调用解决了问题。 My error is to set backlog value too high. 我的错误是将积压值设置得太高。 I not need that server listen more than 5-6 connection. 我不需要那个服务器听超过5-6连接。 I replaced : ServerSocket sock=new ServerSocket(port,500); 我更换了:ServerSocket sock = new ServerSocket(port,500); with ServerSocket sock=new ServerSocket(port,10); 使用ServerSocket sock = new ServerSocket(port,10); And now all work at well! 现在一切顺利!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索java.net.SocketException:没有可用的缓冲区空间 - Hunt down java.net.SocketException: No buffer space available Alfresco共享:java.net.SocketException:没有可用的缓冲区空间 - Alfresco Share: java.net.SocketException: No buffer space available java.net.SocketException:没有可用的缓冲区空间(已达到最大连接?):connect MongoDB JAVA API驱动程序 - java.net.SocketException: No buffer space available (maximum connections reached?): connect MongoDB JAVA API Driver Java套接字服务器提供java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):connect异常 - Java socket server gives java.net.SocketException: No buffer space available (maximum connections reached?): connect exception 使用HTTPBuilder并获得“ java.net.SocketException:没有可用的缓冲区空间(已达到最大连接?):connect” - Using HTTPBuilder and getting “java.net.SocketException: No buffer space available (maximum connections reached?): connect” java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):(添加netstat -a) - java.net.SocketException: No buffer space available (maximum connections reached?):(adding netstat -a) 本地异常失败:java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?): - Failed on local exception: java.net.SocketException: No buffer space available (maximum connections reached?): java.net.SocketException:没有可用的缓冲区空间(已达到最大连接数?):connect - java.net.SocketException: No buffer space available (maximum connections reached?): connect Java-“没有可用的缓冲区空间”套接字错误原因? - Java - “no buffer space available” socket error cause? java.net.SocketException: recv failed: Insufficient buffer space 的原因是什么? - What can be the cause of java.net.SocketException: recv failed: Insufficient buffer space?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM