运行在Nodejs中编写的异步tcp客户端时可能会丢失连接?

时间:2013-09-03 04:28:11

标签: node.js tcp client

这是我用Nodejs编写的压力测试代码,试图让服务器接管30000多个并发连接。

服务器:

var net = require('net');

var clients = 0;

net.createServer(function (sock) {
    ++clients;
    console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort + ', now online: ' + clients);
    sock.on('close', function () {
       --clients;
       console.log('CLOSED, now online: ' + clients);
    });
}).listen(7000);

客户端:

var net = require('net');

var clients = 0;

for (idx = 0; idx < 30000; ++idx) {
    var client = new net.Socket();

    client.connect(7000, 'localhost', function () {
        console.log('connected ' + (++clients));
    });

    client.on('close', function () {
        console.log('Connection closed');
    });
}

运行时,从客户端输出似乎正常,数字一直到30000.但对于服务器端,这个数字最多可达10000+左右。

所以我用C语言编写了一个传统的tcp客户端。而这一次,客户和服务器看起来很好(完全符合预期)。

顺便说一下,在这些测试中没有报告套接字关闭。

有没有人有相同的经历?

0 个答案:

没有答案
相关问题