(结尾)为什么socket.end()
连接上的netcat
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}`)
})
我正在寻找一个可以解决错误的答案,或者说错误的答案,非常清楚地解释为什么会发生错误。
非常感谢,
杰森
var localDate = moment().toDate();
答案 0 :(得分:1)
这与netcat
的某些版本(不是全部)处理TCP连接关闭的原因有关。
完全原因超出了我对TCP的了解,所以我无法帮助你(我只能说ECONNRESET
意味着Node试图从连接中读取已被netcat
)重置/关闭。
对我有用的解决方案是在发送最后一条消息后在套接字上调用destroy()
:
conn.end("Goodbye.\n")
conn.destroy()
我想它会让Node在套接字上执行更多的读操作,这可能会导致错误。