I have a Java client that is supposed to be constantly connected to some Java server and just output whatever the server sends (see excerpt below).
The problem is that if the client computer (MacOS X) goes to sleep and then wakes up again, the client hangs at in.readLine()
and does not realize that the connection is broken. I added a SO_TIMEOUT
to the socket so that the client would stop blocking on readLine()
, and it indeed throws a SocketTimeoutException
, but then happily tries to read a line from the broken connection again.
My questions are: Why doesn't the in.readLine()
fail with an IOException
(so that I can re-initiate the connection) and how can I make the client reconnect to the server on a awake->sleep->awake
cycle on Mac OS X?
Socket socket = new Socket(...);
socket.setSoTimeout(10000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
[...]
while (true) {
try {
while ((serverSays = in.readLine()) != null) {
System.out.println("Server says: " + serverSays);
}
} catch (SocketTimeoutException e) {
continue;
}
// An exception other than SocketTimeoutException will break the while-loop.
}
// If another exception other than SocketTimeoutException occurs,
// catch it and re-initiate the connection and start over.