Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const net = require('net');
- const port = 7070;
- const host = '127.0.0.1';
- 'use strict';
- sockets=[]
- const server = net.createServer();
- server.listen(port, host, () => {
- console.log('TCP Server is running on port ' + port + '.');
- });
- server.on('connection', (sock) => {
- console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
- sockets.push(sock);
- sock.on('data', (data) => {
- console.log('DATA ' + sock.remoteAddress + ': ' + data.toString());
- });
- sock.on('close', (data) => {
- let index = sockets.findIndex(function(o) {
- return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
- })
- if (index !== -1) sockets.splice(index, 1);
- console.log('CLOSED: ' + sock.remoteAddress + ' ' + sock.remotePort);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement