Network timeouts can be caused by high traffic in the networks, slow connections, power interruptions etc. Most of the programmers do not spend their time for detection and handling of network timeouts or leave this task to do at the last moment. But ignoring this situation may cause your application to stop or block the running threads indefinitely.

We never need network timeout management in an ideal world such as in the case of a desktop application or in an intranet application. But in the case of a network application that runs in an internet, good timeout management is critical and the programmer should write code for timeout handling and detection. Users will quickly notice the lack of timeout support while running applications on varying degrees of network qualities. So while developing the application, attention should be given to both network servers and network clients.

By using a timer thread or applying timeout handing with the socket options, we can handle network timeouts. Most of the network programs in server are written as multi threaded applications. However multiple threads can also be used by clients. One solution is to launch a timer thread and have it stop the program gracefully if it is stalled and showing a message will let them know the reason for canceling the application and the timer will exit with this error message. A timer should be reset regularly once it is started, such as when a server returns data. In these type of multi-threaded programs, the application will wait for the timeouts and then perform the required action(like notifying the client or resetting the connection).

Another method is to set socket option, so that programmer get more control over the socket communication. When we use the socket options, give an exception handler to java.io.InterruptedIOException so that we can execute it when an exception is caught. This reduce the length of code to detect timeouts and makes a good design.

Java also support non-blocking network I/O, that can be activated on java.net.Socket, java.net.ServerSocket, or java.net.DatagramSocket. The drawback of non-blocking network I/O is that it requires an existing socket. Here we can specify the maximum timeout period by calling the method java.net.Socket.setSoTimeout(), thereby it blocks the InterruptedIOException before it is thrown. If no method is written specifying the timeout period, then the connection operation will stall for a longer period. Using socket option is the easiest way of handling timeouts.