Socket Set timeout

There are two main ways, we look at
the way 1:

 Socket s=new Socket(); s.connect(new InetSocketAddress(host,port),10000); 
方式2:

 S = the Socket new new the Socket ( "127.0.0.1", 8080 ); s.setSoTimeout ( 10000); 
then timeout of these two methods set each representing what significance does it have? what differences are there?

A first type is
our first look at the first way, we have to test:

In the main method we create Socket connections to

ip: 29.212.19.201, Port: 2132

public static void main(String[] args) {
Socket socket = new Socket();
SocketAddress endpoint = new InetSocketAddress("29.212.19.201", 2132);
long timeMillis = System.currentTimeMillis();
try {
socket.connect(endpoint, 10000);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(System.currentTimeMillis()-timeMillis);
System.out.println("end");
}

  

Run the code, no output before the console 10 seconds, 10 seconds after printing the following information:

10002
java.net.SocketTimeoutException: connect timed out
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.wakling.cn.SocketSever.main(SocketSever.java:33)
end

  

As can be seen, when we try to connect to 29.212.19.201:2132, connecting on 10 seconds are not connected, so he reported java.net.SocketTimeoutException: connect timed out the abnormal.

Explain the above-mentioned IP is an unknown IP, that is not my IP access to the IP network in the current environment, we will go to this Socket been trying to connect to this IP until it times out. If the IP is a known IP, such as local 127.0.0.1 plus an unknown port, then the connection will immediately Socket error.

Further, in the case where the connection time is not provided, Socket default probably 21S (21,020 were tested three times in milliseconds) connection timeout. As the code is not provided in the connection time:

 Socket = Socket new new Socket ( "29.212.19.201", 2132); 
2 ways
and then we look at the second way, this time we need to write a Socket and customer service in our local side to simulate this scenario.

We let the client to set setSoTimeout 10s, after the service request information to get the client-side code, dormant 10s and then process the client request and returns a response.

We look at the results, the key code is as follows:

// server 
System.out.println ( "goes to sleep after waking 10s"); 
the Thread.sleep (10000); 
System.out.println ( "sleep end"); 
// return a response 
OutputStream outputStream = socket.getOutputStream (); // get an output stream, to send information to the server 
PrintWriter printWriter = new PrintWriter (outputStream) ; // packed into the print stream output stream 
printWriter.print ( "Hello, the server has received your message." ); 
printWriter.flush (); 
// client 
the Socket Socket new new = the Socket ( "127.0.0.1", 2132 of); 
socket.setSoTimeout (10000); // Read timeout

  

After running, waiting for client output, the client console output after 10s as follows:

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.wakling.cn.SocketClient.main(SocketClient.java:36)
10020
end

  

Read timed out after the client console to view information normal output, even if the client has reported a timeout, the server continues to go down, but has not received client server 10s: 10s here after being given client java.net.SocketTimeoutException sent its own message.

Also has been tested and found that the server sleep long, long time, such as 500s, when the client does not set setSoTimeout, 120s default timeout.

Difference and significance
here we come to talk about the meaning and difference between the two.

Mode 1 is a timeout the client and the server connection, and can not establish a connection, it reported java.net.SocketTimeoutException namely within 10 seconds: connect timed out connection timeout exception. At this point both the connection is not established, let alone server receives the client's news.

2 embodiment is provided InputStream.read () method of the blocking time, i.e. the request waits for a long time waiting for a response returned from the server to the client sends, over the duration will lead java.net.SocketTimeoutException: Read timed out exception. At this point both normal connection is established, the service receives a client's request.

Two ways to control different timeout focus, like phone calls, method is to call 10 seconds you do not answer the phone I hung up, Method 2 is turned on after the phone call, so you do not speak hung 10 seconds, 10 seconds to say no to speak not listening.

Original link: https: //blog.csdn.net/ibigboy/article/details/93759809

Guess you like

Origin www.cnblogs.com/LeeXiaoYang/p/11494691.html