Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket connect timeouts: where is the specification?

The context of my work is my local area network.

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

I have experienced the following connection timeout:

  •  2 ms when connection established
    
  • 1 005 ms when host is alive but not listen on the specified socket port
  • 21 000 ms when host is down

This values comes from observation of my network but I presume it exists a RFC.

Here is some information about timeout:

  • RFC 1122
  • RFC 793
  • Nagle's_algorithm and TCP_NO_DELAY

Can you give me more pointers?

@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 );
   }
}
like image 658
Aubin Avatar asked Nov 07 '12 09:11

Aubin


People also ask

What is socket connection timeout?

socket timeout — a maximum time of inactivity between two data packets when exchanging data with a server.

How do I set a socket timeout?

Answer: Just set the SO_TIMEOUT on your Java Socket, as shown in the following sample code: String serverName = "localhost"; int port = 8080; // set the socket SO timeout to 10 seconds Socket socket = openSocket(serverName, port); socket. setSoTimeout(10*1000);

What is the default socket timeout?

A timeout value of zero is interpreted as an infinite timeout. The default socket timeout ( SO_TIMEOUT ) in milliseconds which is the timeout for waiting for data. A timeout value of zero is interpreted as an infinite timeout. This value is used when no socket timeout is set in the HTTP method parameters.

What is good value for HTTP socket timeout?

6. Re: Recommended Value for http socket timeout. The http_socket_timeout is generally the one you want to change.


1 Answers

There is no RFC for connection timeouts. It is impossible for any RFC or other document to know the conditions prevailing in any network in advance.

In general you can expect a successful connection to be very quick; an ECONNREFUSED (ConnectException: connection refused) to be about as quick; and a connection timeout (ConnectException: connect timeout) to take as long as it takes, depending on the cause, the platforms at both ends, and the nature of the intervening network. In Windows I believe a connection timeout consists of the total time across three connection attempts with timeouts 6s, 12s, and 24s, total 42s; in various Unixes I believe the total is more like 70s, which could result from 3 attempts with timeouts 10s, 20s, and 40s. As you see it is up to the platform. There is also the issue that filling the backlog queue at a Windows server will cause RSTs to be issued to incoming SYNs, where on a Unix/Linux server it will cause no response at all to incoming SYNs.

You should also note that in Java, and contrary to many years of Javadoc:

  1. A zero connect timeout does not imply an infinite timeout, it implies the platform default timeout, which as shown above isn't above about 70s;

  2. You cannot specify a connection timeout that increases the platform default; you can only use it to decrease the platform default.

like image 133
user207421 Avatar answered Sep 24 '22 14:09

user207421