Socket connection timeouts: where is the specification?

The context of my work is my local network.

The code examples below are written in Java, but my question is about TCP, not programming.

I experienced the following connection timeout:

  • 2 ms when establishing a connection
  • 1 005 ms when the host is alive but not listening on the specified socket port
  • 21,000 ms with no host

These values โ€‹โ€‹come from monitoring my network, but I believe there is an RFC.

The following is information about the timeout:

Can you give me more directions?

@Override public void run() { for( int port = _portFirst; port < _portLast; ++port ) { String host = "192.168.1." + _address; boolean success = false; long before = System.currentTimeMillis(); try { Socket socket = new Socket(); SocketAddress endpoint = new InetSocketAddress( host, port ); socket.connect( endpoint, 0 ); success = true; socket.close(); }// try catch( ConnectException c ){/**/} catch( Throwable t ){ t.printStackTrace(); } long duration = System.currentTimeMillis() - before; System.err.println( host + ":" + port + " = " + duration ); _listener.hostPinged( host, port, success, duration ); } } 
+4
source share
2 answers

No RFC for connection timeouts. For any RFC or other document, it is not possible to know in advance the conditions that exist on any network.

In general, you can expect a successful connection to be very fast; a ECONNREFUSED ( ConnectException: connection refused ) is about as fast; and the connection timeout ( ConnectException: connect timeout ) until it accepts, depending on the reason, the platform at both ends and the nature of the intermediate network. On Windows, I believe that the connection timeout consists of the total time of three connection attempts with timeouts of 6s, 12s and 24s, a total of 42s; on different Unixes, I think the total is more like the 70s, which may be the result of 3 attempts with timeouts of 10, 20 and 40. As you can see, it depends on the platform. There is also the problem that populating the backup queue on a Windows server will result in an RST for inbound SYNs, where on a Unix / Linux server this will not cause any response to inbound SYNs. A.

It should also be noted that in Java and contrary to many years of Javadoc:

  • A timeout with a zero connection does not imply an infinite timeout; it implies a default timeout for the platform, which, as shown above, does not exceed about 70 seconds,

  • You cannot specify a connection timeout that extends the platform by default; you can use it only to reduce the default platform.

+10
source

As you found out, you can specify a timeout in the call to the connect(...) method as such:

 connect( SocketAddress endpoint , int timeout ) 

And remember that ... The zero timeout is interpreted as an infinite timeout โ€ž (whereโ€œ infinite โ€often actually meansโ€œ use the default OS ").

As for the default values, they depend on the OS, but, as a rule, are quite consistent. If you are in the linux window, look at /proc/sys/net/ipv4/tcp_keepalive* (but do not change them if you do not know what you are doing :-)

Greetings

+1
source

Source: https://habr.com/ru/post/1444539/


All Articles