Client Socket timeout (timeout) refers to the network delay, network congestion, and other reasons when the client communicates with the server through Socket, this causes the server to fail to respond to the client in a timely manner. After a period of time, the client throws a timeout error because it has not received the response from the server. The waiting time of the client is the timeout time.
One end of the production timeout error is passive. That is to say, this end is receiving data, rather than sending data. For the client Socket, only two places are receiving data, one is connecting to the server, and the other is when the server receives data sent from the server after the connection is successful. Therefore, there are two types of client Timeout: Connection timeout and data read timeout.
I. Connection timeout
This timeout has been used in the previous example. In the Socket class, only the second parameter of the connect method can specify the connection timeout time. Because you must specify an IP address and port to connect to the server using the connect method, an invalid IP address or port will cause a connection timeout error.
Ii. Data Reading timeout
After successfully connecting to the server, the two most important tasks that the Socket does are "receive data" and "send data". When receiving data, it may be due to network latency, network congestion, and other reasons, the client remains in the waiting state. After the client waits for a period of time, if the server has not sent data to the client, the client Socket will throw a timeout error.
We can use the setSoTimeout method of the Socket class to set the timeout time for Data Reading. The unit of time is milliseconds. This method takes effect only after it is called to read data. If the timeout value is set to 0, the timeout value is not used. That is to say, when the client is disconnected from the server depends entirely on the timeout setting of the server program. The following statement sets the data read timeout to 5 seconds.
Socket socket = new Socket ();
Socket. setSoTimeout (5000 );
Socket. connect (... ...);
Socket. getInputStream (). read ();
Note that do not set the connection timeout and data read timeout to too small. If the value is too small, such as 100, server data may not be sent yet, the client throws a timeout error. The following code provides an example of connection timeout and data read timeout.
Package mynet;
Import java.net .*;
Public class SocketTimeout
{
Public static void main (String [] args)
{
Long time1 = 0, time2 = 0;
Socket socket = new Socket ();
Try
{
If (args. length <4)
{
System. out. println ("parameter error! ");
Return;
}
Time1 = System. currentTimeMillis ();
Socket. connect (new InetSocketAddress (args [0], Integer
. ParseInt (args [1]), Integer. parseInt (args [2]);
Socket. setSoTimeout (Integer. parseInt (args [3]);
Time1 = System. currentTimeMillis ();
Socket. getInputStream (). read ();
}
Catch (SocketTimeoutException e)
{
If (! Socket. isClosed () & socket. isConnected ())
System. out. println ("data read timeout! ");
Else
System. out. println ("connection timeout ");
}
Catch (Exception e)
{
System. out. println (e. getMessage ());
}
Finally
{
Time2 = System. currentTimeMillis ();
System. out. println (time2-time1 );
}
}
}
The main method of the SocketTimeout class requires four parameters: IP (domain name), port, connection timeout, and data read timeout. Let's test this example with a set of data.
Test 1: timeout error caused by invalid IP Address
Run the following command:
Java mynet. SocketTimeout 192.168.18.24 80 3000 5000
Running result:
Connection timeout
3045
Note: 192.168.18.24 is an invalid IP address. If the IP address exists in the network environment, replace it with another invalid IP address. In this test case, invalid IP addresses cannot be replaced with invalid domain names. This is because if a domain name is used to connect to the server, Java will first map the domain name to the corresponding IP address through DNS; if the domain name is invalid, an error occurs during the ing process. Therefore, the program will not perform server connection operations and will naturally not throw a "connection timeout" error.
Test 2: timeout error caused by Invalid Port
Run the following command:
Java mynet. SocketTimeout www.ptpress.com.cn 8888 3000 5000
Running result:
Connection timeout
3075
Test 3: data read timeout error
Run the following command:
Java mynet. SocketTimeout www.ptpress.com.cn 80 3000 5000
Running result:
Data read timeout!
5008
Test 4: Set the read data timeout value to 0
Run the following command:
Java mynet. SocketTimeout www.ptpress.com.cn 80 3000 0
Running result:
Connection reset
131519
The output results of the previous three tests show that connection timeout and read data timeout are set to 3000 and 5000 milliseconds respectively in each test case; their running results are not 3000 and 5000 milliseconds, but swing around the set timeout time. This is mainly because the system output time is not always the timeout time; some time may be the time for Java to handle errors and output information to the console. In addition, the system timing error also affects the accuracy of the time-out period. However, the timeout always changes around the set time.
For test 4, after the read data timeout is set to 0, the SocketTimeout class throws a Connection reset error after more than two minutes (131519 milliseconds. The error throw time is related to the timeout setting of the server program. That is, this error is generated because the server program actively disconnects the client network.