const net = require('net'); const client = new net.Socket(); client.connect(PORT, HOST, function() { console.log('Connected'); client.write('Hello, server! Love, Client.'); }); client.on('data', function(data) { console.log('Received: ' + data); client.destroy(); // kill client after server's response }); client.on('close', function() { console.log('Connection closed'); });
const net = require('net'); const server = net.createServer(function(socket) { // on connection console.log('New client connected'); socket.write('Hello client!'); socket.on('data', function(data) { console.log('Received: ' + data); socket.write('You sent: ' + data); }); socket.on('end', function() { console.log('Client disconnected'); }); }); server.listen(PORT, function() { console.log('Server listening on port ' + PORT); });Package Library: The net module is part of the Node.js core library and does not require external package libraries.