Skip to content

Instantly share code, notes, and snippets.

@conradz
Created September 28, 2011 20:45
Show Gist options
  • Save conradz/1249213 to your computer and use it in GitHub Desktop.
Save conradz/1249213 to your computer and use it in GitHub Desktop.
Node TCP Test
var net = require('net');
var host = '127.0.0.1';
var port = 8888;
var server = net.createServer(function(client) {
console.log('Client connected');
client.on('data', function(data) {
client.write(data);
});
});
server.listen(port, host);
console.log('Server listening at ' + host + ':' + port);
var client = net.createConnection(port, host, function() {
console.log('Connection successful');
var recieved = '';
var sent = 'This is a test';
client.write(sent);
client.on('data', function(data) {
recieved += data.toString();
if (recieved === sent) {
client.destroy();
server.close();
console.log('Recieved data');
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment