-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp-server.js
120 lines (103 loc) · 3.4 KB
/
tcp-server.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const net = require("net");
var HOST = "127.0.0.1";
var PORT = 6969;
// Create a TCP/IPC server instance, and chain the listen function to it
// net
const server = net.createServer();
server.listen(PORT, HOST, () => {
console.log("TCP Server listening on " + HOST + ":" + PORT);
});
let sockets = [];
server.on("connection", function (socket) {
console.log("CONNECTED: " + socket.remoteAddress + ":" + socket.remotePort);
sockets.push(socket);
// Loop through all sockets and write them to one console.table()
sockets.forEach(function (socket) {
console.table({
"Connected Clients": socket.remoteAddress + ":" + socket.remotePort,
});
});
socket.on("data", function (data) {
console.log(
"DATA " +
socket.remoteAddress +
":" +
socket.remotePort +
": " +
data +
"\n"
);
sockets.forEach(function (s) {
s.write(s.remoteAddress + ":" + s.remotePort + " sent: " + data);
});
});
// Add a 'close' event handler to this instance of socket
socket.on("close", function (data) {
console.log("CLOSED: " + socket.remoteAddress + ":" + socket.remotePort);
let index = sockets.findIndex(function (o) {
return (
o.remoteAddress === socket.remoteAddress &&
o.remotePort === socket.remotePort
);
});
if (index !== -1) sockets.splice(index, 1);
});
socket.on("error", function (err) {
console.log("ERROR: " + err.message);
});
socket.on("end", function () {
console.log("ENDED SESSION");
});
});
// net
// .createServer(function (socket) {
// // When a client connects, we note it in the console
// console.log("CONNECTED: " + socket.remoteAddress + ":" + socket.remotePort);
// socket.write("Welcome to the server!\n");
// socket.write("To quit the server type 'quit/kill'\n");
// socket.write("Please enter your message: ");
// socket.setEncoding("utf8");
// socket
// // When the client sends data, we print it in the console
// .on("data", function (data) {
// if (
// data.toString().toLowerCase() === "quit" ||
// data.toString().toLowerCase() === "kill"
// ) {
// socket.end();
// }
// console.log("DATA RECEIVED: " + data + "\n");
// socket.write("You sent: " + data + "\n");
// socket.write("Please enter your message: \n");
// })
// .on("end", function () {
// console.log(
// "CLOSED: " + socket.remoteAddress + " " + socket.remotePort
// );
// })
// .on("error", function (err) {
// console.log("ERROR: " + err);
// })
// .on("close", function () {
// console.log(
// "CLOSED: " + socket.remoteAddress + " " + socket.remotePort
// );
// })
// .on("drain", function () {
// console.log("DRAIN: " + socket.remoteAddress + " " + socket.remotePort);
// })
// .on("timeout", function () {
// setTimeout(10000, function () {
// console.log(
// "TIMEOUT: " + socket.remoteAddress + " " + socket.remotePort
// );
// socket.end();
// });
// })
// .on("end", function () {
// console.log("END: " + socket.remoteAddress + " " + socket.remotePort);
// });
// })
// .listen(PORT, HOST, () => {
// console.log("TCP Server listening on " + HOST + ":" + PORT);
// });