Share this page 

Have timeout on socket connectionTag(s): Networking

ServerSocket server = new ServerSocket(port);
// timeout after 60 seconds
try {
  Socket socket=server.accept();
catch ( e ) {
  System.err.println( "Timed Out (60 sec)!" );
This is true for READ operation too. Since READ operation blocks as long necessary it may be wise to use the setSoTimeout() method. Note that when the TIMEOUT expires, an InterruptException is thrown. However, the socket is still connected even though the Exception was raised.