-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
42 lines (33 loc) · 1.05 KB
/
client.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
const tcp = require("net");
const udp = require("dgram");
const config = require("./config");
function sendCountToTCPPort(count, port) {
[...new Array(count)].forEach(() => {
const client = new tcp.Socket();
client.connect(port, config.ip, function() {
client.write("Hello, server! Love, Client.");
});
client.on('data', (data) => {
console.log(`TCP ${port} ${data.toString()}`);
client.destroy();
})
});
}
function sendCountToUDPPort(count, port) {
[...new Array(count)].forEach(() => {
const client = udp.createSocket("udp4");
const data = Buffer.from("Hello, server! Love, Client.");
client.send(data, port, config.ip);
client.on("message", (data) => {
console.log(`UDP ${port} ${data.toString()}`);
client.close();
})
});
}
const maximumMessagesCount = 4;
config.tcpPorts.forEach(port => {
sendCountToTCPPort(Math.ceil(Math.random() * maximumMessagesCount), port);
});
config.udpPorts.forEach(port => {
sendCountToUDPPort(Math.ceil(Math.random() * maximumMessagesCount), port);
});