如何解决java net ConnectException 连接被拒绝连接甚至服务器已启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20242841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:31:25  来源:igfitidea点击:

How to resolve java net ConnectException Connection refused connect even server is up

javasocketshttpurlconnection

提问by Murali

I am using Httpurlconnection to send request from my jboss server to my device. The Device has been build up by cgi.

我正在使用 Httpurlconnection 将请求从我的 jboss 服务器发送到我的设备。设备已由 cgi 构建。

When server sends request from multiple thread at a time to device, some thread got an exception as java.net.ConnectException: Connection refused. but the device is sending response to server in some delay. I have set httpurlconnection timeout as 30 seconds(3000 milliseconds).

当服务器一次从多个线程向设备发送请求时,某些线程会出现异常,如 java.net.ConnectException:连接被拒绝。但设备在一些延迟后向服务器发送响应。我已将 httpurlconnection 超时设置为 30 秒(3000 毫秒)。

But the error has come only when multiple thread sending it at same time.

但是只有在多个线程同时发送时才会出现错误。

Does any one know please guide me to resolve the problem.

有谁知道请指导我解决问题。

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:519)
    at java.net.Socket.connect(Socket.java:469)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:852)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:793)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:718)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1041)

采纳答案by MadConan

If a single thread connects fine, every time, but some threads running concurrently get this exception, then this is most likely due to a limited number of connections available. Can you change how many concurrent connections are available on the device? If not, then you can try limiting the number of threads that attempt to make the connection.

如果单个线程每次都连接正常,但一些并发运行的线程会出现此异常,那么这很可能是由于可用连接数量有限。您能否更改设备上可用的并发连接数?如果没有,那么您可以尝试限制尝试建立连接的线程数。

EDIT: If you can't modify the "device" application or configure it for debugging, then try to see the exact behavior of the threads that are attempting to connect to it. Use a debugger on the client (which is your jboss server in this case) or log debug info from the threads showing connect and release times to see if the exception alwaysoccurs when multiple threads are attempting to connect simultaneously (as opposed to threads finishing the connection before other threads connect or timeout).

编辑:如果您无法修改“设备”应用程序或对其进行配置以进行调试,请尝试查看尝试连接到它的线程的确切行为。在客户端上使用调试器(在这种情况下是您的 jboss 服务器)或从显示连接和释放时间的线程中记录调试信息,以查看当多个线程尝试同时连接时是否总是发生异常(而不是线程完成连接)在其他线程连接或超时之前连接)。

Ifit turns out that it really is a connection limit causing the issue, you could try creating a singleton connection object that is shared among threads. This will seriously bottle neck the jboss application when multiple threads attempt to connect. If that isn't acceptable, you'll have to come up with a new solution (e.g. multiple devices, write your own app, etc).

如果事实证明它确实是导致问题的连接限制,您可以尝试创建一个在线程之间共享的单例连接对象。当多个线程尝试连接时,这将严重阻碍 jboss 应用程序。如果这是不可接受的,您将不得不想出一个新的解决方案(例如,多个设备、编写您自己的应用程序等)。