- Save kfox/1280c2f0ee8324067dba15300e0f2fd3 to your computer and use it in GitHub Desktop.
TCP echo server for Node.js
Usage
- Make sure you have a modern-ish version of Node.js installed.
- Type
npx https://gist.github.com/kfox/1280c2f0ee8324067dba15300e0f2fd3 - Connect to it from a client, e.g.
netcator similar:nc localhost 9000
| #!/usr/bin/env node | |
| import net from 'node:net' | |
| const port = process.argv[2] || 9000 | |
| const server = net.createServer((connection) => { | |
| const { address, port } = connection.address() | |
| const socket = `${address}:${port}` | |
| console.log(`${socket} - client connected`) | |
| connection.on('data', (data) => { | |
| console.log(`${socket} - client sent: %s`, String(data).trim()) | |
| connection.write(data) | |
| }) | |
| connection.on('end', () => { | |
| console.log(`${socket} - client disconnected`) | |
| }) | |
| }) | |
| server.listen({ port }, () => { | |
| const { address, port } = server.address() | |
| console.log('listening on %s:%d', address, port) | |
| }) |