How to write a TCP Echo server in Javascript

If c is the most verbose way to write a tcp server. Javascript (nodejs) is probably the least verbose way.

var net = require("net");

var server = net.createServer(function (socket) {
  socket.pipe(socket);
});

server.listen(process.argv[2], "127.0.0.1");

Not only is this code much more simple to understand (in my opinion) but it also gives us multiple clients for free - because of nodejs’s event loop architecture.

Related Posts