Skip to content

Instantly share code, notes, and snippets.

@jasonkuhrt
Created September 8, 2016 19:23
Show Gist options
  • Save jasonkuhrt/5d3d4acb8a17e3f5079e23b2e525dfe7 to your computer and use it in GitHub Desktop.
Save jasonkuhrt/5d3d4acb8a17e3f5079e23b2e525dfe7 to your computer and use it in GitHub Desktop.
node-tcp-netcat-error
import TCP from "net"
import Promise from "Bluebird"
const log = console.log
/* TODO
WHY IS THIS HAPPENING?
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
*/
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}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment