Skip to content

Instantly share code, notes, and snippets.

@jkrems
Created March 7, 2014 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkrems/9422148 to your computer and use it in GitHub Desktop.
Save jkrems/9422148 to your computer and use it in GitHub Desktop.
Number doubling service
'use strict';
var net = require('net');
var LineInputStream = require('line-input-stream');
var END_MESSAGE = new Buffer("Thank you for using the nodejs doubling service\n");
var server = net.createServer(function(socket) {
var processLine = function(line) {
if (line.trimRight() === 'end') {
socket.end(END_MESSAGE);
} else {
var j = parseInt(line);
if (isNaN(j)) {
socket.write("not an int: <" + line + ">\n");
} else {
socket.write((j * 2) + "\n");
}
}
};
new LineInputStream(socket).on('line', processLine);
socket.on('timeout', function() {
socket.end('timeout');
});
socket.on('error', function(err) {
// silently ignore error
});
});
server.listen(44444);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment