I was hoping someone might be able to give me a recommendation on how best to handle a socket timeout. We have implemented a special InputStream which has a safe read method. The purpose of this functionality is to read a specified number of bytes sent from a server but if not all bytes are read we loop waiting until the remaining bytes are received. However we also set the socket timeout using Socket.setSoTimeout to ensure that we do not block forever waiting for remaining expected bytes. Now if the socket timeout expires a java.io.InterruptedIOException is thrown. My issue is with how to handle this exception. What we really want to do is catch this exception and reestablish the socket connection to the server (our logic being that if the server only send us part of a message then somthing has gone seiously wrong on the server side and we will drop the current connection and create another). However if we always catch this InterruptedIOException on the assumption that the server only sent a partial message and we want to create a new connection what happens when our thread is blocked in a read and someone tries to shutdown the application. I understand that this will also result in an InterruptedIOException. So in this case our thread should be shutting down and we dont want to go and try to recreate the socket connection. How can we handle these seperate situations i.e. the InterruptedIOException that occurs when a socket read times out and the InterruptedIOException that occurs when we try to stop a thread while the socket is blocked in a read.
Thank you Catalin. This is exactly the type of behaviour I would have expected. Im not sure how I got confused about the issue as I have just noticed that the socket API states that a SocketTimeoutException will be thrown if the timeout expires. Cheers..
subject: Catching InterruptedIOException from Socket timeout