Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created November 16, 2012 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fideloper/4087879 to your computer and use it in GitHub Desktop.
Save fideloper/4087879 to your computer and use it in GitHub Desktop.
Echo server (node.js)
var net = require("net"), sys = require('sys');
var clients = [], i;
var server = net.createServer(function (stream) {
stream.setEncoding("utf8");
stream.on("connect", function () {
clients.push(stream);
});
stream.on("data", function (data) {
console.log(data + "sending to " + clients.length + " clients.");
for(i = 0; i < clients.length; i++){
if(clients[i] != stream){
clients[i].write(data + "\0");
}
}
});
stream.on("end", function () {
stream.end();
for(i = 0; i < clients.length; i++){
if(clients[i] == stream){
clients.splice(i, 1);
}
}
console.log('removed client');
});
});
process.on('uncaughtException', function(exception){
console.log("uncaught, just ignoring");
});
console.log("Server's running on 192.168.1.198:8080");
server.listen(8080);
@tmtanzeel
Copy link

I want to create a simple client that will connect to this server and get replies echoed back on command prompt itself. No fancy HTML CSS. Please help me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment