简体   繁体   English

Java HttpUrlConnection连接超时由于连接被拒绝而无法正常工作

[英]Java HttpUrlConnection connection time out is not working due to connection refused

Hi I am working on Java application and deployed on Jboss ,windows 8 server. 嗨,我正在研究Java应用程序,并部署在Jboss Windows 8服务器上。 In my server code i use HttpUrlconnection for communicating with my products. 在我的服务器代码中,我使用HttpUrlconnection与我的产品进行通信。

I would like to set a time limit for evey connection to connect with my product. 我想设置evey连接与我的产品连接的时间限制。 So that i use HttpUrlconnection.setconnectionTimeout(30000) for 30 seconds time limit. 这样我就使用HttpUrlconnection.setconnectionTimeout(30000) 30秒的时间限制。 But the connection has been refused at 9 minutes. 但是连接已在9分钟被拒绝。 HttpUrlconnection.setconnectionTimeout() is not working at this time. HttpUrlconnection.setconnectionTimeout()目前无法正常工作。

I have found the problem that has been happened by TCP Timeout and retransmission limit has been set in windows 8 server. 我发现TCP超时已发生问题,并且已在Windows 8服务器中设置了重传限制。 By default TCP retransmission limit is 2. after 2 tries, the connection will be abandoned or refused. 默认情况下,TCP重传限制为2。尝试2次后,连接将被放弃或拒绝。

My question is that How to avoid this problem from java side without changing Windows registry files? 我的问题是,如何在不更改Windows注册表文件的情况下从Java方面避免此问题? It means that have any possiblities to settimeout or retry TCP SYN/RST/ACK mechanism to avoid connection refused problem. 这意味着可以设置settimeout或重试TCP SYN / RST / ACK机制,以避免连接被拒绝的问题。

Does any one know, please help me to solve this issue 有谁知道吗,请帮我解决这个问题

url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection)  
url.openConnection();
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setConnectTimeout(30000
connection.setReadTimeout(30000;

try {
    // Get Response
    inputStream = connection.getInputStream();
} catch (IOException e) {
    //              
}

The Exception is below 异常在下面

java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.<init>(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.http.HttpClient.New(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

That Connection Refused means that there is no program at the other end of the connection that listens to/expects your connections. 拒绝连接意味着在连接的另一端没有程序可以监听/期待您的连接。 If your products will open up later, you will need to retry the connection manually; 如果您的产品以后可以打开,则需要手动重试连接;否则,请重新启动。 there is no wait to make a connection attempt 'linger' so that it gets connected when the program starts. 无需等待连接尝试“徘徊”,以便在程序启动时连接。

To be honest, it looks like you have your connection logic ass-backwards: it looks like you are trying to connect from your long-running program (server) to short-running ones (clients); 坦白地说,您看起来像是向后倾斜了连接逻辑:看起来您正在尝试从长时间运行的程序(服务器)连接到短期运行的程序(客户端); reverse that and make the server listen for conenctions and the clients should connect to the server when they start. 扭转这种情况,并使服务器侦听连接,并且客户端在启动时应连接到服务器。

Connection timeouts are for the case where there is no response at all. 连接超时适用于根本没有响应的情况。 A connection refusal is a response, so the timeout doesn't apply. 连接拒绝是一种响应,因此超时不适用。

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

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