-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletcpserver.js
More file actions
executable file
·30 lines (23 loc) · 1016 Bytes
/
simpletcpserver.js
File metadata and controls
executable file
·30 lines (23 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env node
const net = require('net');
const port = 3004;
const server = net.createServer();
server.listen(port, function() {
console.log(`Server listening for connection requests on socket localhost:${port}`);
});
server.on('connection', function(socket) {
console.log(`connected ${socket.remoteAddress}:${socket.remotePort}`);
socket.write(`Hello ${socket.remoteAddress}:${socket.remotePort}!!\n\n`);
socket.on('data', function(chunk) {
if(!chunk || !`${chunk}`.match(/mypassword/))socket.destroy();
console.log(`Data received from ${socket.remoteAddress}:${socket.remotePort} => ${chunk.toString()}`);
//socket.write(`Response to what you said: ${data}`);
socket.destroy();
});
socket.on('end', function() {
console.log(`Closing connection with ${socket.remoteAddress}:${socket.remotePort}`);
});
socket.on('error', function(err) {
console.log(`Error from: ${socket.remoteAddress}:${socket.remotePort}`);
});
});