【问题标题】:Node TCP server error ECONNRESET with netcat clients带有 netcat 客户端的节点 TCP 服务器错误 ECONNRESET
【发布时间】:2016-09-08 19:33:58
【问题描述】:

在示例 NodeJS 服务器(最后)中,为什么(似乎)netcat 连接上的 socket.end() 会导致 ECONNRESET 服务器错误?

观察,带有客户端的服务器上的套接字对象错误:

TERMINAL 1
netcat localhost 9000
Hello.
Goodbye.

TERMINAL 2
==> A Connection connected
==> The Server is disconnecting a connection
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

现在观察,带有客户端的服务器上没有套接字对象错误:

TERMINAL 1
telnet localhost 9000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Hello.
Goodbye.
Connection closed by foreign host.

TERMINAL 2
==> A Connection connected
==> The Server is disconnecting a connection
==> A Connection disconnected

我正在寻找可以修复错误的答案,或者如果失败,请非常清楚地解释为什么会发生这种情况。

非常感谢,
杰森

import TCP from "net"
import Promise from "Bluebird"

const log = console.log



const server = TCP.createServer((conn) => {

  conn.setEncoding('utf8')

  log("==> A Connection connected")

  conn.write("Hello.\n", async function () {
    await Promise.delay(3000)
    if (!conn.destroyed) {
      log("==> The Server is disconnecting a connection")
      conn.end("Goodbye.\n")
    }
  })

  conn.on("error", log)

  conn.on("end", () => {
    log("==> A Connection disconnected")
  })

  conn.on("data", (message) => {
    log("==> A Connection says:\n%s", message)
  })
})



const port = 9000

server.listen(port, () => {
  log(`==> The Server is listening on port ${port}`)
})

【问题讨论】:

  • 感谢您的链接。如果您想获得答案 - 请在此处包含您的代码
  • @baao 好的,我已经复制了代码,谢谢。

标签: node.js tcp netcat


【解决方案1】:

原因与netcat 的某些版本(不是全部)如何处理TCP 连接的关闭有关。

确切原因超出了我对 TCP 的了解,所以我无法帮助您(我只能说ECONNRESET 表示 Node 试图从netcat 已重置/关闭)。

对我有用的解决方案是在发送最后一条消息后在套接字上调用destroy()

conn.end("Goodbye.\n")
conn.destroy()

我猜它表示 Node 无法在套接字上执行更多读取,这可能会导致错误。

【讨论】:

  • 我会等待接受更详细的答案,但这是一个有用的开始,谢谢!
猜你喜欢
  • 2013-08-12
  • 1970-01-01
  • 2012-12-12
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
相关资源
最近更新 更多