4

使用 .net 时,我注意到尝试连接到未侦听的端口总是需要 1 秒。为了检查这是否是 .net 抽象中的问题或是否是较低级别的问题,并将其与 linux 进行比较(其中不成功telnet需要大约 3 毫秒),我使用 node.js 脚本连接到端口

  • 12345,没有进程正在侦听的端口
  • 80,正在监听的端口

Windows的结果:

Connecting to 127.0.0.1:12345
#3: error elapsed: 1000ms, Error: connect ECONNREFUSED
#2: error elapsed: 1002ms, Error: connect ECONNREFUSED
#4: error elapsed: 1003ms, Error: connect ECONNREFUSED
#1: error elapsed: 1007ms, Error: connect ECONNREFUSED
#0: error elapsed: 1015ms, Error: connect ECONNREFUSED

Connecting to 127.0.0.1:80
#0: connect elapsed: 8ms
#1: connect elapsed: 1ms
#2: connect elapsed: 3ms
#3: connect elapsed: 4ms
#4: connect elapsed: 6ms

Linux的结果:

Connecting to 127.0.0.1:12345
#4: error elapsed: 0ms, Error: connect ECONNREFUSED
#3: error elapsed: 1ms, Error: connect ECONNREFUSED
#2: error elapsed: 1ms, Error: connect ECONNREFUSED
#1: error elapsed: 1ms, Error: connect ECONNREFUSED
#0: error elapsed: 3ms, Error: connect ECONNREFUSED

Connecting to 127.0.0.1:80
#4: connect elapsed: 0ms
#3: connect elapsed: 0ms
#2: connect elapsed: 0ms
#1: connect elapsed: 1ms
#0: connect elapsed: 2ms

Node.js源码

var net = require('net');
var host = process.argv[2];
var port = Number(process.argv[3]);
console.log("Connecting to %s:%d", host, port);
for (i = 0; i < 5; i++)
{
  (function(i) {
    var date = +new Date;
    var client = net.connect({host: host, port: port});
    client.on('error', function(msg)
    {
      console.log("#%d: error elapsed: %dms, %s", i, new Date - date, msg);
    });
    client.on('connect', function()
    {
      console.log("#%d: connect elapsed: %dms", i, new Date - date);
    });
  })(i);
}

一些笔记

  • 我也尝试了远程 IP 地址,结果与上面显示的 localhost 类似
  • 在 node.js 和我编写它的方式的情况下,(异步)连接尝试并行运行,但在 .net 的情况下,我正在同步测试,即一次又一次的尝试,结果是相同的:每次1秒

知道为什么Windows在这里很慢吗?这是某种刻意的节流吗?

4

2 回答 2

4

@HarryJohnston 的评论让我重写程序来看看

如果另一个程序在那一秒延迟期间开始侦听该端口会发生什么

而且,在 Windows 上,对 connect() 的调用似乎以 500 毫秒的间隔尝试连接到未绑定端口最多 3 次。如果第三次尝试也失败了,它就会放弃。

这是测试运行的日志,我偶尔随机启动和停止侦听端口 12345 的进程:

Connecting to 192.168.77.11:12345
#1: error elapsed: 1008ms, Error: connect ECONNREFUSED
#2: error elapsed: 1001ms, Error: connect ECONNREFUSED
#3: error elapsed: 1004ms, Error: connect ECONNREFUSED
#4: connect elapsed: 1001ms
#5: connect elapsed: 1000ms
#6: connect elapsed: 500ms
#7: error elapsed: 1000ms, Error: connect ECONNREFUSED
#8: connect elapsed: 1001ms
#9: error elapsed: 1000ms, Error: connect ECONNREFUSED
#10: connect elapsed: 500ms
#11: error elapsed: 1000ms, Error: connect ECONNREFUSED
#12: error elapsed: 1000ms, Error: connect ECONNREFUSED
#13: connect elapsed: 500ms
#14: error elapsed: 1000ms, Error: connect ECONNREFUSED
#15: connect elapsed: 500ms
#16: error elapsed: 1000ms, Error: connect ECONNREFUSED
#17: connect elapsed: 500ms
#18: connect elapsed: 1000ms
#19: connect elapsed: 1000ms
#20: connect elapsed: 500ms
#21: connect elapsed: 1011ms
#22: connect elapsed: 1000ms
#23: error elapsed: 1000ms, Error: connect ECONNREFUSED

以及修改后的源代码:

var net = require('net');
var host = process.argv[2];
var port = Number(process.argv[3]);
console.log("Connecting to %s:%d", host, port);
var connect = function(i) {
    var date = +new Date;
    var client = net.connect({host: host, port: port});
    client.on('error', function(msg)
    {
      console.log("#%d: error elapsed: %dms, %s", i, new Date - date, msg);
      connect(i + 1);
    });
    client.on('connect', function()
    {
      console.log("#%d: connect elapsed: %dms", i, new Date - date);
      connect(i + 1);
    });
  };
connect(1);
于 2013-10-18T04:18:29.623 回答
3

根据Microsoft 的这篇文章,原因是 RFC 让实现决定在连接被拒绝时准确执行什么操作,而在 Windows 上,默认设置是在失败前重试 3 次。

于 2019-02-08T20:49:05.287 回答