【问题标题】:How to catch ECONNRESET error from 'net' module in node js? And prevent whole program from getting terminated?如何从节点 js 中的“net”模块中捕获 ECONNRESET 错误?并防止整个程序被终止?
【发布时间】:2021-01-01 22:23:01
【问题描述】:

如果我尝试 NodeJS 文档中的非常基本的代码

//CLIENT.js
    const net = require('net');
    const client = net.createConnection({ port: 8124 }, () => {
      // 'connect' listener.
      console.log('connected to server!');
      client.write('I am client');
    });
    client.on('data', (data) => {
      console.log(data)
      client.write('I am client');
      //client.end(); I don't want to close the connection
    });
    client.on('end', () => {
      console.log('disconnected from server');
    });

//SERVER.js
    const net = require('net');
    const server = net.createServer((c) => {
      // 'connection' listener.
      console.log('client connected');
      c.on('end', () => {
        console.log('client disconnected');
      });
      c.on('data', (data) => {
        console.log(data)
        c.write("I am server");
      })
      c.pipe(c);
    });
    server.on('error', (err) => {
      console.log("Something went worng"); //I hope this will be triggered in cases of error
    });
    server.listen(8124, () => {
      console.log('server bound');
    });

现在的问题:-

  • 我启动 SERVER.js node SERVER.js
  • 然后我在另一个 CMD 选项卡上启动 CLIENT.js node CLIENT.js
  • 到目前为止一切顺利......'我是服务器我是客户端............'

注意我使用的是 Windows 10

  • 现在,我只需按ctrl + C 即可关闭客户端程序,然后服务器端程序终止并抛出“read ECONNRESET”

完整的错误日志

events.js:292
  throw er; // Unhandled 'error' event

Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNRESET',
  code: 'ECONNRESET',
  syscall: 'read'
}

我理解为什么理论上会导致这个错误,所以我尝试以不同的方式解决它,因为程序没有触发server.on('error')事件,它只是终止了整个服务器程序。

所以我尝试的是

//SERVER.js

    const net = require('net');
    const server = net.createServer((c) => {
      // 'connection' listener.
      console.log('client connected');
try {
      c.on('end', () => {
        console.log('client disconnected');
      });
      c.on('data', (data) => {
        console.log(data)
        c.write("I am server");
      })
      c.pipe(c);
} catch(e){
//hoping of catching the error
console.log("Here is your error", e)
// !!! But no error is caught here and the program still gets completely terminated
}
    });
    server.on('error', (err) => {
      console.log("Something went worng"); //I hope this will be triggered in cases of error
    });
    server.listen(8124, () => {
      console.log('server bound');
    });

那么我应该如何处理“read ECONNRESET”错误呢?

【问题讨论】:

    标签: node.js networking error-handling tcp


    【解决方案1】:

    如果您环绕 try-catch 整个代码并在 catch 中再次调用 main/starting 函数,它将再次启动,但它将进入 try-catch 的无限循环,直到您获得 ECONNRESET 的服务为又不起来了,

    确保您的应用程序所依赖的服务运行时间达到 99.99999%,并让您的应用程序失败并重新启动。 (例如,使用 PM2 类型的实用程序)

    【讨论】:

    • 我在 try 块中尝试了整个代码(没有循环),但在 catch 中没有显示任何内容,并且程序以完全相同的错误终止:-(
    • 但要注意我只有在同一台计算机上执行客户端和服务器时才会收到此错误,如果我在 ngrok 的 tunned 端口上运行服务器,那么我不会收到此错误。但也不会触发“断开连接”事件。
    • 可能是你为服务器和客户端使用相同的端口,在节点中我们也有 process.uncaughtexception 可以捕获任何错误
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2015-10-02
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多