Socket connect timeouts: where is the specification?

前端 未结 2 1421
鱼传尺愫
鱼传尺愫 2021-02-10 08:15

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 experi

相关标签:
2条回答
  • 2021-02-10 08:40

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

    connect( SocketAddress endpoint , int timeout )
    

    And remember that "...A timeout of zero is interpreted as an infinite timeout." (where "infinite" often really means "use the OS default").

    As for the defaults, they are OS dependent, but usually fairly consistent anyways. If you're on a linux box, have a look at /proc/sys/net/ipv4/tcp_keepalive* (but don't change them unless you know what you're doing :-)

    Cheers,

    0 讨论(0)
  • 2021-02-10 08:57

    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.

    0 讨论(0)
提交回复
热议问题