Created
November 15, 2015 13:02
-
-
Save rudiedirkx/ca68ffe2722d07effab6 to your computer and use it in GitHub Desktop.
Super simple socket listener in Node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net'); | |
function log(msg) { | |
process.stdout.write(msg.trim() + "\n", 'utf8'); | |
} | |
var port = Number(process.argv[2]); | |
if ( !port || isNaN(port) ) { | |
return log('Need valid PORT number as argument.'); | |
} | |
net.createServer(function(socket) { | |
log('New connection!'); | |
socket.setEncoding('utf8'); | |
socket.on('data', function(data) { | |
data = data.trim(); | |
log('Incoming: ' + data.length + ' bytes'); | |
log('"' + data + '"'); | |
this.write('abc'); | |
}); | |
socket.on('end', function() { | |
log('Connection closed'); | |
}); | |
}).listen(port, function() { | |
log('Starting server...'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment