0

Hi I am working on Java application and deployed on Jboss,windows 8 server. In my server code i use HttpUrlconnection for communicating with my products.

I would like to set a time limit for evey connection to connect with my product. So that i use HttpUrlconnection.setconnectionTimeout(30000) for 30 seconds time limit. But the connection has been refused at 9 minutes. HttpUrlconnection.setconnectionTimeout() is not working at this time.

I have found the problem that has been happened by TCP Timeout and retransmission limit has been set in windows 8 server. By default TCP retransmission limit is 2. after 2 tries, the connection will be abandoned or refused.

My question is that How to avoid this problem from java side without changing Windows registry files? It means that have any possiblities to settimeout or retry TCP SYN/RST/ACK mechanism to avoid connection refused problem.

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)
Simulant
  • 19,190
  • 8
  • 63
  • 98
Murali
  • 341
  • 2
  • 7
  • 24

2 Answers2

0

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Hi. i got your point. But Due to firewall or some other reasons i was not able to connect my device. at that time When i call http request it sends SYN packets to device through TCP when invoking java.net.PlainSocketImpl.socketConnect(Native Method) . It tries to get acknowledge from device. it tries only two time . because windows registered TcpMaxDataRetransmissions is 2. after two time failure it abandons request. so exception is occured. Is it possible to extend retry or extend TCP timeout by java? – Murali Dec 18 '13 at 12:35
  • I can't make head or tail of that. I can only repeat. If there was a response of any kind before the timeout elapses, the timeout doesn't apply. That includes SYN/ACK and RST. – user207421 Dec 19 '13 at 00:38