Skip to content

Instantly share code, notes, and snippets.

@getnamo
Last active October 16, 2018 20:45
Show Gist options
  • Save getnamo/7350f00823f46d9463240160320d03a3 to your computer and use it in GitHub Desktop.
Save getnamo/7350f00823f46d9463240160320d03a3 to your computer and use it in GitHub Desktop.
/** from: https://nodejs.org/api/net.html#net_server_address */
const net = require('net');
const util = require('util');
const port = 3000;
let server = net.createServer((socket) => {
//give it an id
socket.id = Math.floor(Math.random() * 1000);
//console.log(util.inspect(socket));
console.log('Socket connected as ' + socket.id);
socket.write('connected as ' + socket.id);
socket.on('data',(data)=>{
console.log('got data: ' + data);
socket.write('echo ' + data + '\r\n');
});
socket.on('error',(err)=>{
console.log(socket + 'err ' + err);
});
socket.on('close',()=>{
console.log(socket.id + ' disconnected.');
});
});
server.listen(port);
console.log('TCP listening at port ' + port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment