Right now I’m doing this:
while (true) {
try {
SocketAddress sockaddr = new InetSocketAddress(ivDestIP, ivDestPort);
downloadSock = new Socket();
downloadSock.connect(sockaddr);
this.oos = new ObjectOutputStream(downloadSock.getOutputStream());
this.ois = new ObjectInputStream(downloadSock.getInputStream());
break;
} catch (Exception e) {}
}
downloadSock.connect(sockaddr) will generate a ConnectionRefused exception if the remote host is not listening on the socket. I’m running my code in a separate thread, so I’m not worried about blocking. Given this, is my method of retrying appropriate or is there a better way???
Thanks!
In this case I usually use a progressively longer sleep period each request.
It could be that the server is almost up, so you just want to try again in a second. But if that request fails, wait 2 seconds, but if that one fails, wait 4, etc.
It may be that you want to cap the amount of waiting to 30 seconds or a minute or something like that. It’s probably wise to define the maximum number of tries so you don’t just wait indefinitely.
Something like this might calculate your next delay in seconds: