-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUDP.js
More file actions
37 lines (28 loc) · 816 Bytes
/
UDP.js
File metadata and controls
37 lines (28 loc) · 816 Bytes
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
/**
* Sending messages via UDP (User diagram protocol)
*
*/
const ADDRESS = 8001
const dgram = require('dgram');
const server = dgram.createSocket('udp4');
server.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
server.close();
});
server.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
server.on('listening', () => {
const address = server.address();
console.log(`server listening ${address.address}:${address.port}`);
});
// server listening 0.0.0.0:41234
server.bind(ADDRESS);
setTimeout(() => {
const message = Buffer.from('Some bytes');
const client = dgram.createSocket('udp4');
client.send(message, ADDRESS, 'localhost', (err) => {
client.close();
console.log('Sent message')
});
}, 2000);