套接字连接超时:规范在哪里? [英] Socket connect timeouts: where is the specification?

查看:173
本文介绍了套接字连接超时:规范在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作环境是我的局域网。

The context of my work is my local area network.

下面的代码示例是用Java语言编写的,但我的问题是关于TCP,而不是编程。

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

我遇到以下连接超时:


  • 建立连接后2 ms

  • 主机处于活动状态但没有侦听指定套接字端口时的1 005 ms

  • 主机关闭时21 000 ms

这个值来自我的网络观察,但我认为它存在一个RFC。

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

你能给我更多指示吗?

@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 );
   }
}


推荐答案

那里没有用于连接超时的RFC。任何RFC或其他文档都不可能提前知道任何网络中的主要条件。

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.

一般来说,您可以期望连接成功非常快;一个 ECONNREFUSED ConnectException:连接被拒绝)快得多;并且连接超时( ConnectException:connect timeout )根据原因,两端的平台以及中间网络的性质进行。在Windows中,我认为连接超时包括三次连接尝试的总时间,超时为6s,12s和24s,总共42s;在各种Unix中,我认为总数更像是70s,这可能是因为3次尝试超时10s,20s和40s。如您所见,这取决于平台。还有一个问题是,在Windows服务器上填充积压队列将导致RST被发送到传入的SYN,在Unix / Linux服务器上,它将不会对传入的SYN产生任何响应。

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.

您还应该注意到在Java中,与Javadoc多年相反:

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


  1. 零连接超时并不意味着无限超时,它意味着平台默认超时,如上所示不超过70秒;

  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;

您无法指定连接超时,增加平台默认值;你只能用它来降低平台默认值。

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

这篇关于套接字连接超时:规范在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆