-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
50 lines (41 loc) · 1.51 KB
/
app.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
const udp = require('dgram');
const fs = require('fs');
const path = require('path');
// Constants that you may change
const port = 12345;
const output_file_name = `output_${new Date().getTime()}.json`;
// Create the writing Stream file:
let stream;
// --------------------creating a udp server --------------------
// creating a udp server
const server = udp.createSocket('udp4');
// emits when any error occurs
server.on('error', function (error) {
console.log('Error: ' + error);
server.close();
});
// emits on new datagram msg
server.on('message', function (msg, info) {
// console.log('Data received from client : ' + msg.toString());
// console.log('Received %d bytes from %s:%d\n', msg.length, info.address, info.port);
stream.write(msg);
});
//emits when socket is ready and listening for datagram msgs
server.on('listening', function () {
const address = server.address();
const port = address.port;
const family = address.family;
const ipaddr = address.address;
const file_save_path = path.join(__dirname.toString(),'outputs',output_file_name);
// Write Stream File:
stream = fs.createWriteStream(file_save_path);
console.log('Server is listening at port: ' + port);
console.log('Server ip: ' + ipaddr);
console.log('Server is IP4/IP6: ' + family);
console.log('Server saves the output to: ' + file_save_path);
});
//emits after the socket is closed using socket.close();
server.on('close', function () {
console.log('Socket is closed !');
});
server.bind(port);