GeeCON Prague 2014*
The moose likes Sockets and Internet Protocols and the fly likes Catching InterruptedIOException from Socket timeout Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Catching InterruptedIOException from Socket timeout" Watch "Catching InterruptedIOException from Socket timeout" New topic
Author

Catching InterruptedIOException from Socket timeout

John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Hi all,

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.

Cheers John.

Here is our "safe" read method

Catalin Merfu
Ranch Hand

Joined: May 26, 2004
Posts: 42
try
{
...
}
catch( SocketTimeoutException timeoutEx )
{
// close client socket and reconnect
}
catch( InterruptedIOException interEx )
{
// exit application
}


Catalin Merfu<br /><a href="http://www.accendia.com" target="_blank" rel="nofollow">High Performance Java Networking</a>
John Ryan
Ranch Hand

Joined: Mar 14, 2001
Posts: 124
Originally posted by Catalin Merfu:
try
{
...
}
catch( SocketTimeoutException timeoutEx )
{
// close client socket and reconnect
}
catch( InterruptedIOException interEx )
{
// exit application
}


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..
 
GeeCON Prague 2014
 
subject: Catching InterruptedIOException from Socket timeout