Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created August 7, 2012 05:37
Show Gist options
  • Save heapwolf/3281985 to your computer and use it in GitHub Desktop.
Save heapwolf/3281985 to your computer and use it in GitHub Desktop.
tcp/client-server
var net = require('net');
var sock;
var port = 8089;
if(process.argv[2] === 'client') {
sock = new net.Socket();
sock.meta = 'foobar';
sock.on('data', function(data) {
console.log(String(data));
});
sock.connect(port);
sock.write('ping');
}
if(process.argv[2] === 'server') {
sock = new net.Server();
sock.listen(port);
sock.on('connection', function(socket) {
socket.on('data', function(data) {
console.log(String(data));
socket.write('pong');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment