我正在测试我可以连接到我的服务器的多少并发套接字(具有12 GB RAM的第4代6核intel)。我在Node.js中编写应用程序。服务器只接受连接,然后它告诉我连接了多少并发连接。客户端只需创建一个套接字并连接。在客户端我试图创建15,000个并发连接但服务器似乎在1000到3000个连接之间的任何地方失败(连接被拒绝)。我在我的服务器上使用Ubuntu 14.04,并在/etc/security/limits.conf文件中将我的软限制和硬限制提高到100,000。
myUser hard nofile 100000
myUser soft nofile 100000
我不确定导致问题的原因是什么,如何允许更多并发用户?我很确定这不是硬件限制,因为我相信12GB内存应该可以连接超过1000-3000个连接。
我的客户档案:
var net = require('net');
var HOST = '192.168.1.83';
var PORT = 5000;
for (var i=0; i<15000; i++){
var client = new net.Socket();
client.connect(PORT, HOST, function() {
console.log('CONNECTED TO: ' + HOST + ':' + PORT);
});
client.on('data', function(data) {
});
// Add a 'close' event handler for the client socket
client.on('close', function() {
console.log('Connection closed');
});
}
我的服务器文件:
// Load the TCP Library
var net = require('net');
// Keep track of the chat clients
var clients = [];
var numClients = 0;
// Start a TCP Server
net.createServer(function (socket) {
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort ;
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
numClients++;
console.log(numClients);
// Handle incoming messages from clients.
socket.on('data', function (data) {
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
numClients--;
console.log(numClients);
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
}).listen(5000);
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5000\n");