Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Created August 6, 2012 17:09
Show Gist options
  • Save heapwolf/3276706 to your computer and use it in GitHub Desktop.
Save heapwolf/3276706 to your computer and use it in GitHub Desktop.
streams/duplex
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