Java Socket Read Timeout Example
Java Socket setSoTimeout(int timeout)
PreviousNext
Java Socket setSoTimeout(int timeout) Enable/disable (SocketOptions#SO_TIMEOUT SO_TIMEOUT) with the specified timeout, in milliseconds.
Introduction
Enable/disable (SocketOptions#SO_TIMEOUT SO_TIMEOUT) with the specified timeout, in milliseconds.
With this option set to a positive timeout value, a read() call on the InputStream associated with this Socket will block for only this amount of time.
If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid.
A timeout of zero is interpreted as an infinite timeout.
The option must be enabled prior to entering the blocking operation to have effect.
Syntax
The method setSoTimeout() from Socket is declared as:
public synchronized void setSoTimeout(int timeout) throws SocketException Parameter
The method setSoTimeout() has the following parameter:
- int timeout - the specified timeout, in milliseconds.
Exception
The method setSoTimeout() throws the following exceptions:
- SocketException - if there is an error in the underlying protocol, such as a TCP error
- IllegalArgumentException - if timeout is negative
Example
The following code shows how to use Java Socket setSoTimeout(int timeout)
Example 1
import java.net.Socket; public class Main { public static void main(String[] args) throws Exception { Socket client = new Socket("google.com", 80); client.setSoTimeout(1000); System.out.println(client.getSoTimeout()); client.close(); } }
Result
Example 2
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.util.StringTokenizer; public class Main { public static void main(String[] arguments) throws Exception { StringTokenizer split = new StringTokenizer(arguments[0], "@"); String user = split.nextToken(); String host = split.nextToken(); Socket digit = new Socket(host, 79); digit.setSoTimeout(20000);/ / w w w . d e m o 2 s . c o m PrintStream out = new PrintStream(digit.getOutputStream()); out.print(user + "\015\012"); BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream())); boolean eof = false; while (!eof) { String line = in.readLine(); if (line != null) System.out.println(line); else eof = true; } digit.close(); } }
Example 3
//package je3.net; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.SocketTimeoutException; /**/ / w w w . d e m o 2 s . c o m * A simple network client that establishes a network connection to a specified * port on a specified host, send an optional message across the connection, * reads the response from the server and exits. A suitable client for simple * network services like the daytime or finger. */ public class Main { public static void main(String[] args) { try { // Handle exceptions below // Get our command-line arguments String hostname = args[0]; int port = Integer.parseInt(args[1]); String message = ""; if (args.length > 2) for (int i = 2; i < args.length; i++) message += args[i] + " "; // Create a Socket connected to the specified host and port. Socket s = new Socket(hostname, port); // Get the socket output stream and wrap a PrintWriter around it PrintWriter out = new PrintWriter(s.getOutputStream()); // Sent the specified message through the socket to the server. out.print(message + "\r\n"); out.flush(); // Send it now. // Get an input stream from the socket and wrap a BufferedReader // around it, so we can read lines of text from the server. BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); // Before we start reading the server's response tell the socket // that we don't want to wait more than 3 seconds s.setSoTimeout(3000); // Now read lines from the server until the server closes the // connection (and we get a null return indicating EOF) or until // the server is silent for 3 seconds. try { String line; while ((line = in.readLine()) != null) // If we get a line System.out.println(line); // print it out. } catch (SocketTimeoutException e) { // We end up here if readLine() times out. System.err.println("Timeout; no response from server."); } out.close(); // Close the output stream in.close(); // Close the input stream s.close(); // Close the socket } catch (IOException e) { // Handle IO and network exceptions here System.err.println(e); } catch (NumberFormatException e) { // Bad port number System.err.println("You must specify the port as a number"); } catch (ArrayIndexOutOfBoundsException e) { // wrong # of args System.err.println("Usage: Connect <hostname> <port> message..."); } } }
PreviousNext
Related
- Java Socket getSendBufferSize()
- Java Socket setSoLinger(boolean on, int linger)
- Java Socket getSoLinger()
- Java Socket setSoTimeout(int timeout)
- Java Socket getSoTimeout()
- Java Socket setTcpNoDelay(boolean on)
- Java Socket getTcpNoDelay()
Source: https://www.demo2s.com/java/java-socket-setsotimeout-int-timeout.html
0 Response to "Java Socket Read Timeout Example"
Post a Comment