【发布时间】: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 好的,我已经复制了代码,谢谢。