July 2017
Intermediate to advanced
656 pages
16h 1m
English
First, we'll create our server.
In server.js, let's write the following:
const net = require('net') net.createServer((socket) => { console.log('-> client connected') socket.on('data', name => { socket.write(`Hi ${name}!`) }) socket.on('close', () => { console.log('-> client disconnected') }) }).listen(1337, 'localhost')
Now for the client, our client.js should look like this:
const net = require('net') const socket = net.connect(1337, 'localhost') const name = process.argv[2] || 'Dave' socket.write(name) socket.on('data', (data) => { console.log(data.toString()) }) socket.on('close', () => { console.log('-> disconnected by server') })
We should be able to start our server and connect to it with our client. ...