in the example NodeJS server (at end) why does (it seems) socket.end() on a netcat connection cause a ECONNRESET server error?
Observe, a socket-object error on server with client:
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' }
Now observe, no socket-object error on server with client:
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
I'm looking for an answer that will fix the error, or failing that, very clearly explain why this must happen.
Many thanks,
Jason
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}`)
})