Created
September 28, 2011 20:45
-
-
Save conradz/1249213 to your computer and use it in GitHub Desktop.
Node TCP Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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