【问题标题】:Why Node js tcp server not detecting disconnected client为什么 Node js tcp 服务器没有检测到断开连接的客户端
【发布时间】:2017-07-11 11:04:57
【问题描述】:

我正在尝试使用节点 js 中的 TCP-IP 进行服务器-客户端通信

下面是我的服务器端代码,我有一个充当客户端的 GSM 设备。当 GSM 设备连接到服务器时,我会收到设备已连接的消息!但是当我切断 GSM 设备的电源时,服务器应该会识别出设备已断开连接,但即使我有断开连接事件的代码,屏幕上也不会显示任何消息。

服务器代码

// Load the TCP Library
net = require('net');

// Keep track of the chat clients
var clients = [];

// 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");
 broadcast(socket.name + " Device is connected!\n", socket);

 // Handle incoming messages from clients.
 socket.on('data', function (data) {
 broadcast(socket.name + "> " + data, socket);
 });

 // Remove the client from the list when it leaves
 socket.on('end', function () {
 clients.splice(clients.indexOf(socket), 1);
 broadcast(socket.name + " Device left.\n");
 });

 // 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");

【问题讨论】:

  • TCP 非常健壮,因为它会等待很长时间(通常是几个小时),然后才能宣布连接失效。您需要实现某种 ping/pong 类型的协议,服务器尝试定期向客户端发送消息以查看连接是否仍处于活动状态。只有socket.setKeepAlive,但由操作系统决定连接在多长时间后被认为是死的(同样,这可能是几个小时)。
  • @robertklep 同样的问题,我使用了socket.setKeepAlive(true, 60000); //1 min = 60000 milliseconds. 它检测到客户端已断开连接但抛出异常Exception has occurred: Error Error: read ECONNRESET at exports._errnoException (util.js:1018:11) at TCP.onread (net.js:568:26) 以及如何在多客户端连接中查找谁已断开连接
  • @robertklep 是的,如果我使用 socket.setKeepAlive,我也会遇到同样的问题
  • @Valar_Dohaeris 就像我说的那样,由操作系统决定在什么时间之后它会认为连接失效,即使使用保持活动的数据包也是如此。对于 Linux,有一些 sysctl 值决定了这一点。
  • @R.zeiwald 听起来您没有在套接字中添加 error 处理程序。

标签: node.js sockets tcp tcp-ip


【解决方案1】:
// Load the TCP Library
net = require('net');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function(socket) {
try {
    socket.setKeepAlive(true, 600); //1 min = 60000 milliseconds.
} catch (exception) {
    console.log('exception', exception);
}
socket.on('error', onError.bind({}, socket));

function onError(socket) {

    //console.log('Socket error!', socket);
    console.log('name', socket.name);


}
// 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");
broadcast(socket.name + " joined\n", socket);

// Handle incoming messages from clients.
socket.on('data', function(data) {
    console.log(data);
    broadcast(socket.name + "> " + data, socket);
});

// Remove the client from the list when it leaves
socket.on('end', function() {
    clients.splice(clients.indexOf(socket), 1);
    broadcast(socket.name + " left .\n");
});

// 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(8003);

 // Put a friendly message on the terminal of the server.
 console.log("Chat server running at port 8003\n");

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    • 2014-01-24
    • 1970-01-01
    相关资源
    最近更新 更多