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.
The end of the timeout error is passive. That is to say, this end is receiving data, not 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, no timeout value is 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 timeout 3045
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 the connection after more than two minutes (131519 milliseconds ).
Reset error. The error throw time is related to the timeout settings of the server program. That is, this error is generated when the server program actively disconnects the client network.