Java Socket Timeout
Chapter:
Networking
Last Updated:
10-12-2019 15:25:37 UTC
Program:
/* ............... START ............... */
import java.io.IOException;
import java.net.Socket;
public class JavaSocketTimeout {
public static void main(String[] args) throws IOException {
Socket socket = new Socket(); // Making a socket object
//integer timeout value
int timeout=30000000;
/* setSoTimeout() method enables or disables the SO_TIMEOUT option with the given
timeout value, in milliseconds. */
socket.setSoTimeout(timeout);
//getSoTimeout() method returns the setting for SO_TIMEOUT option
System.out.println("Timeout value: "+socket.getSoTimeout());
}
}
/* ............... END ............... */
Output
Notes:
-
Socket timeout is used set the timeout session , after this time interval connection will reset.
- In java setSoTimeout() function is used to set the socket timeout. Syntax : socket.setSoTimeout(time in milli seconds);
- A timeout of zero is interpreted as an infinite timeout.
- SocketTimeoutException will throw if timeout expires before connecting.
Tags
java socket timeout, java socket timeout exception