Not sure why 'java.net.ConnectException: Connection timed out: connect' exception is occuring
The below thread is in a loop and it just conn开发者_运维知识库ects to a server, downloads a file, closes the connection and then repeats the process. The below exception is thrown after approx 500 iterations of the loop -
java.net.ConnectException: Connection timed out: connect
Why might this be occurring? Is there a way to kill the thread once the exception is thrown ?
Code -
public void run() {
boolean isExceptionThrown = false;
try {
while(true){
URL url = new URL(urlString);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
{
//do nothing, just want to read the file
}
in.close();
++counter;
System.out.println("Thread id : "+this.threadId+" Connection number : "+counter +" URL : "+urlString);
}
}
catch(Exception e){
e.printStackTrace();
}
}
Thanks.
The problem is that you never allow your connections to fully close your Reader
once and letting the thread free resources for the next while
loop:
There might be too many connection in a thread pool that creating another 1 (while others are trying to close) creates a timeout (that's my guess).
Do this instead (pseudo java code):
while (true) {
BufferedReader in = null;
try {
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception ignore) {
ignore.printStackTrace();
}
}
in = null;
}
}
Also, give your thread time to "clean" itself before it's completely free from activity. Try to sleep
/wait for the thread to completely close a connection and free its resources.
Caveat: speculating here. Even though your client code is closing the connection, your server might not be cleaning up quite so quickly, hence your client isn't able to establish a new connection. It certainly sounds like at the ~500 connections mark the server is a bit overwhelmed.
Try putting in something like a sleep for 10s when count == 400 to see if that makes a difference to how many you can process?
It could be that the server is getting swamped by too many connections. Even if you close the stream it's possible that the server takes some time to shut down the socket.
The code you have will exit the loop once an exception is thrown because the catch
block is outside the while (true)
block. Once run
returns the thread will die.
First of all I think the thread will exit after printing the stack trace so you should have the kill the thread.
Regarding the reasons, there is not enough information in your question to find out what's the real issue. I think you should also check on the server side to check if downloading forever the same file do not raise an issue on the server side or trigger a protective measure.
It you want to be sure you can use something like Wireshark to check the network activity between the client and the server. You'll probably notice that on the last connection you'll see outbound packets but not inbound ones.
精彩评论