Skip to content

Instantly share code, notes, and snippets.

@droidMakk
Created December 5, 2017 03:28
Show Gist options
  • Save droidMakk/18448ce79428319ab0c7c51ed5f78d7e to your computer and use it in GitHub Desktop.
Save droidMakk/18448ce79428319ab0c7c51ed5f78d7e to your computer and use it in GitHub Desktop.
Simple NodeJS TCP Client & Server
//----------- Server Code------------------------
var net = require('net');
var srv = net.createServer(function(socket){
socket.on('data',function(data){
console.log(data.toString());
})
socket.end('Love, from server')
}).on('error',function(err){
throw err;
})
srv.listen({
host: 'localhost',
port: 80
},function(){
console.log('Listening at 80')
})
//----------------Client Code----------------------
var net = require('net')
var client = net.createConnection({
port: 80,
host: "127.0.0.1"
},function(){
console.log("Connected to server")
client.write("off");
})
client.on('data',function(){
console.log(data.toString())
})
client.on('end',function(){
console.log('Ended connection')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment