-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpServer.js
43 lines (35 loc) · 1.1 KB
/
udpServer.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
const chalk = require('chalk');
const dgram = require('dgram');
let count = 0;
let bytes = 0;
let errors = 0;
function reportStats(prefix) {
console.log(prefix+`${chalk.blueBright(''+bytes)} bytes, in ${chalk.green(''+count)} messages, ${chalk.red(errors)} errors.`);
}
function udpServer(options) {
const {host, port, repeats, interval, verbose, quiet} = options;
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
errors++;
console.log(`Server error (${err.code}): ${err.message}`);
reportStats('Error after ');
});
server.on('message', (msg, rinfo) => {
// console.log(`Received from ${rinfo.address}:${rinfo.port}, received: ${msg} `);
count++;
bytes += msg.length;
if (!quiet) {
reportStats('Received ');
}
if ((repeats > 0) && (count >= repeats)) {
reportStats('Test complete: received ');
server.close();
}
});
server.on('listening', () => {
const address = server.address();
console.log(`Listening on ${address.address}:${address.port}`);
});
server.bind(port, host);
}
module.exports = { udpServer };