Skip to content

Instantly share code, notes, and snippets.

@ollym
Created June 28, 2010 22:41
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 ollym/eb46af138d604dabb5c7 to your computer and use it in GitHub Desktop.
Save ollym/eb46af138d604dabb5c7 to your computer and use it in GitHub Desktop.
// We want a tcp socket
var socket = require('net').socket.tcp;
// Instantiate a new socket listening on port 8010 at 'localhost'
var sock = new socket(8010, 'localhost')
// Listen out for incoming connections
socket.listen(function(stream) {
// Log the connection
console.log('connection from address: ' + stream.endpoint());
// Set no delay
stream.delay = false;
stream.timeout = 0;
stream.encoding = 'utf8';
// Means data is buffered when written and flush() needs to be called to send the data
stream.autoflush = false;
// Listen for data
stream.events.bind('data', function(data) {
// Log the incoming message
console.log('message received from address: ' + stream.endpoint() + ' message: ' + data);
// Write separate messages
stream.write('thank you!');
stream.write('very much!');
// Flush the message
stream.flush();
// stream.close(true) would close the stream and flush!
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment