My Java client hangs in the constructor for Socket when it tries to connect to a server that is down. Is there a way to force this to timeout?
Created May 4, 2012
Tim Rohaly This is a typical problem with distributed programs - this is
not a problem unique to Java. If your server is down, then attempting
to create a socket connection to it will hang until the
underlying transport layer times out. Under Windows, this will
occur in about 45 seconds. There is no way to change this timeout period
in Java (setSoTimeout() works only after the socket has been
created, and only affects read() calls).
If this behavior is not sufficient for your
needs, you will have to create a watchdog thread that interrupts
your socket after a certain time.