Repository navigation
TCP
TCP Server:
'use strict';
// load the Node.js TCP library
const net = require('net');
const PORT = 1234;
const HOST = 'localhost';
class Server {
constructor(port, address) {
this.port = port || PORT;
this.address = address || HOST;
this.init();
}
init() {
let server = this;
let onClientConnected = (sock) => {
let clientName = ${sock.remoteAddress}:${sock.remotePort};
console.log(new client connected: ${clientName});
sock.on('data', (data) => {
console.log(${clientName} Says: ${data});
sock.write(data);
sock.write('exit');
});
sock.on('close', () => {
console.log(connection from ${clientName} closed);
});
sock.on('error', (err) => {
console.log(Connection ${clientName} error: ${err.message});
});
}
server.connection = net.createServer(onClientConnected);
server.connection.listen(PORT, HOST, function() {
console.log(Server started at: ${HOST}:${PORT});
});
}
}
module.exports = Server;
TCP Client:
'use strict';
const net = require('net');
const PORT = 1234;
const HOST = 'localhost';
class Client {
constructor(port, address) {
this.socket = new net.Socket();
this.address = address || HOST;
this.port = port || PORT;
this.init();
}
init() {
var client = this;
client.socket.connect(client.port, client.address, () => {
console.log(Client connected to: ${client.address} : ${client.port});
client.socket.write('Hello World!');
});
client.socket.on('data', (data) => {
console.log(Client received: ${data});
if (data.toString().endsWith('exit')) {
client.socket.destroy();
}
});
client.socket.on('close', () => {
console.log('Client closed');
});
client.socket.on('error', (err) => {
console.error(err);
});
}
}
module.exports = Client;