简体   繁体   English

java.net.ConnectException:连接被拒绝超时

[英]java.net.ConnectException: Connection refused timeout

I saw a lot of "java.net.ConnectException: Connection refused" questions but none referring to timeout of this error.我看到了很多“java.net.ConnectException:连接被拒绝”的问题,但没有一个是指这个错误的超时。 My problem is I have to connect to a server that, in some cases, is blocked (connected by another software to the same port).我的问题是我必须连接到在某些情况下被阻止的服务器(由另一个软件连接到同一端口)。 So, I'm doing a loop with some max retries to try to connect:因此,我正在循环进行一些最大重试以尝试连接:

My current code (of course, is depending on a lot of configurations for my software, but is working fine):我当前的代码(当然,取决于我的软件的很多配置,但工作正常):

    public TCPConnector(TCPDefinition tcpDefinition) throws IAException {
    ivTcpDefinition = tcpDefinition;
     // Initialize the socket
    boolean retry = false;
    int counter = 1;
    do {
        try {
            ivSocket = new Socket();
            ivSocket.connect(new InetSocketAddress(tcpDefinition.getHostname(), tcpDefinition.getPort()), tcpDefinition.getConnectTimeOut());
            ivSocket.setSoTimeout(tcpDefinition.getAckTimeOut());
            retry = false;
        }
        catch (UnknownHostException uhe) {
            throw new IAException(null, new StringBuffer("Can't find host: ").append(tcpDefinition.getHostname()).toString(), uhe);
        }
        catch (SocketException see) {
            StringBuilder sb = new StringBuilder("Connection refused to host ").append(tcpDefinition.getHostname()).
            append(" port ").append(tcpDefinition.getPort()).append(". Connection Attempt Nr. ").append(counter);
            logger.error(sb.toString(), see);
            retry = true;
            if (counter++ > tcpDefinition.getConnectRetries())
                throw new IAException(null, sb.toString(), see);
            else
                logger.error("will retry to connect");
        }
        catch (IOException ioe) {
            StringBuilder sb = new StringBuilder("I/O error while connecting to host ").append(tcpDefinition.getHostname()).
            append(" port ").append(tcpDefinition.getPort()).append(". Connection Attempt Nr. ").append(counter);
            logger.error(sb.toString(), ioe);
            retry = true;
            if (counter++ > tcpDefinition.getConnectRetries())
                throw new IAException(null, sb.toString(), ioe);
            else
                logger.error("will retry to connect");
        }
    }
    while (retry);

}

Well, the problem is this:嗯,问题是这样的:

  • On Windows, every second, the SocketException is thrown, instead the IOException, while I have configured a timeout of 5000 msec to ivSocket.connect在 Windows 上,每秒都会抛出 SocketException,而不是 IOException,而我已经为ivSocket.connect配置了 5000 毫秒的超时
  • On Linux, this is thrown every millisecond!!在 Linux 上,这是每毫秒抛出一次!!

Windows:视窗:

2019-12-05 12:40:47,609 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2019-12-05 12:40:47,609 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接拒绝托管本地主机端口 13002。连接尝试编号。 1 java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) 1 java.net.ConnectException: 连接被拒绝:在 java.net.PlainSocketImpl.socketConnect(Native Method) 连接

2019-12-05 12:40:48,703 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2019-12-05 12:40:48,703 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接拒绝托管本地主机端口 13002。连接尝试编号。 2 java.net.ConnectException: Connection refused: connect 2 java.net.ConnectException:连接被拒绝:连接

Linux: Linux:

2019-12-05 12:45:47,609 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2019-12-05 12:45:47,609 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接拒绝托管本地主机端口 13002。连接尝试编号。 1 java.net.ConnectException: Connection refused: connect at java.net.PlainSocketImpl.socketConnect(Native Method) 1 java.net.ConnectException: 连接被拒绝:在 java.net.PlainSocketImpl.socketConnect(Native Method) 连接

2019-12-05 12:45:47,610 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - Connection refused to host localhost port 13002. Connection Attempt Nr. 2019-12-05 12:45:47,610 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接拒绝托管本地主机端口 13002。连接尝试编号。 2 java.net.ConnectException: Connection refused: connect 2 java.net.ConnectException:连接被拒绝:连接

Why the timeout is not executed?为什么超时没有执行? Well this is not exactly right.嗯,这不完全正确。 If I configure a timeout less than 1 second on Windows, then the timeout is executed.如果我在 Windows 上配置了小于 1 秒的超时,则执行超时。 500 msec: 500 毫秒:

2019-12-05 11:47:07,375 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - I/O error while connecting to host localhost port 13002. Connection Attempt Nr. 2019-12-05 11:47:07,375 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接到主机本地主机端口 13002 时出现 I/O 错误。连接尝试编号。 1 java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) 1 java.net.SocketTimeoutException: 在 java.net.PlainSocketImpl.socketConnect(Native Method) 连接超时

2019-12-05 11:47:07,875 ERROR DefaultQuartzScheduler_Worker-1 TCPConnector - I/O error while connecting to host localhost port 13002. Connection Attempt Nr. 2019-12-05 11:47:07,875 错误 DefaultQuartzScheduler_Worker-1 TCPConnector - 连接到主机本地主机端口 13002 时出现 I/O 错误。连接尝试编号。 2 java.net.SocketTimeoutException: connect timed out at java.net.PlainSocketImpl.socketConnect(Native Method) 2 java.net.SocketTimeoutException: 在 java.net.PlainSocketImpl.socketConnect(Native Method) 连接超时

It is possible to configure a "connect refuse" timeout?是否可以配置“连接拒绝”超时?

There is no such thing as a "connection refused timeout".没有“连接拒绝超时”之类的东西。

"Connection refused" happens when the server sees the connection request, but there is no service listening for connections on the IP + port that the request is directed to.当服务器看到连接请求,但没有服务侦听请求指向的 IP + 端口上的连接时,就会发生“连接被拒绝”。 The server then "refuses" the connection.然后服务器“拒绝”连接。 This typically happens instantly, so so no timeout is triggered.这通常会立即发生,因此不会触发超时。

"Connection timed out" happens (typically) when something stops the connection request from reaching the server 1, 2 .当某些东西阻止连接请求到达服务器1, 2时,就会发生“连接超时”(通常)。 So the client-side will wait for the response from the server, and then resend / wait a few times.所以客户端会等待来自服务器的响应,然后重新发送/等待几次。 And eventually the time allotted for establishing a connection will expire ... and the connection times out.最终分配给建立连接的时间将到期......并且连接超时。

As you can see these are different scenarios.如您所见,这些是不同的场景。 And they are reported back to the Java client-side differently.并且它们以不同的方式报告回 Java 客户端。

So the reason you are not getting timeouts is that the "connection refused" responses are coming back quick enough that your configured timeout is not exceeded.因此,您没有超时的原因是“连接被拒绝”响应返回的速度足够快,不会超过您配置的超时。

That might also explain why setting the connect timeout small might have changed the behavior.这也可以解释为什么将连接超时设置得小可能会改变行为。 There may also be issues with the granularity of the timeout that the OS allows Java to set.操作系统允许 Java 设置的超时粒度也可能存在问题。

To investigate this further, I think we would need a minimal reproducible example.为了进一步研究这一点,我认为我们需要一个最小的可重复示例。 For example, we need to see how you have implemented the code that manages the server-socket and accepts connections on the server side.例如,我们需要看看您是如何实现管理服务器套接字和接受服务器端连接的代码的。


1 - The blockage could be on the server's reply packets. 1 - 阻塞可能在服务器的回复数据包上。
2 - There are various possible causes for this kind of thing. 2 - 这种事情有多种可能的原因。 The most likely are a firewall blocking traffic somewhere, a network routing problem, or using a private IP address on the wrong network.最有可能的原因是防火墙在某处阻止了流量、网络路由问题或在错误的网络上使用了私有 IP 地址。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM