Working with .net, I noticed that an attempt to connect to a port that is not listened on always takes one 1 second. To check whether this is a problem in the .net abstraction or whether it is a lower level issue, and to compare it with linux (where an unsuccessful telnet
takes something like 3ms), I used a node.js script to connect to port
- 12345, a port no process is listening on
- 80, a port that is being listened on
Results for 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
Results for 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 source:
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);
}
A couple of notes
- I also tried remote IP addresses, the results are similar to those shown above for localhost
- in the case of node.js and the way I wrote it, the (asynchronous) connection attempts run in parallel, but in case of .net, I was testing synchronously, i.e. one attempt after the other, and the result was the same: 1sec every time
Any idea why Windows is slow here? Is that some kind of deliberate throttling?