Last active
July 3, 2019 14:47
-
-
Save gnosis23/5b09be2c8fdc14db119bbfadf38d9a09 to your computer and use it in GitHub Desktop.
networks
This file contains 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
const net = require('net'); | |
const readline = require('readline'); | |
const client = net.connect({port: 8124}, function () { | |
console.log('client connected'); | |
}); | |
client.on('end', function () { | |
console.log('client disconnected'); | |
}); | |
client.on('data', function (data) { | |
console.log(data.toString()); | |
}); | |
client.on('error', function(err) { | |
console.log(err); | |
process.exit(-1); | |
}) | |
// readline | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
prompt: '> ' | |
}); | |
// enter interactive | |
rl.prompt(); | |
rl.on('line', (line) => { | |
client.write(line); | |
rl.prompt(); | |
}).on('close', () => { | |
console.log('Have a great day!'); | |
client.end(); | |
}); |
This file contains 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 log = console.log; | |
var server = net.createServer(function (socket) { | |
// new connection | |
socket.on('data', function (data) { | |
const info = data.toString(); | |
socket.write(info); | |
log(`received: ${info}`); | |
}); | |
socket.on('end', function () { | |
log('client disconnected'); | |
}); | |
socket.write('hello\r\n'); | |
}); | |
server.on('error', function (err) { | |
throw err; | |
}); | |
server.listen(8124, function () { | |
log('server bound, 8124') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment