-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (38 loc) · 1.48 KB
/
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
//sources
//https://nodejs.org/api/dgram.html#udpdatagram-sockets
//https://stackoverflow.com/questions/8393636/configure-node-js-to-log-to-a-file-instead-of-the-console
//https://www.freecodecamp.org/news/javascript-get-current-date-todays-date-in-js/
//https://flaviocopes.com/fix-dirname-not-defined-es-module-scope/
import dgram from 'node:dgram';
import fs from 'fs';
import util from 'util';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const SERVER_PORT = 41234;
const server = dgram.createSocket('udp4');
const date = new Date().toJSON().slice(0,10);
const logFile = fs.createWriteStream(__dirname + '/udp_server' + date + '.log', {flags : 'a'});
server.on('error', (err) => {
console.error(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
const logData = 'server got: ' + msg + ' from ' + rinfo.address +':' + rinfo.port + ', event at:' + new Date().toJSON();
console.log(logData);
logFile.write(util.format(logData) + '\n');
const response = Buffer.from('Message Received')
server.send(response, rinfo.port, rinfo.address, (err) => {
if (err) {
console.error('Failed to send response !!')
} else {
console.log('Response send Successfully')
}
})
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
server.bind(SERVER_PORT);