-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
30 lines (21 loc) · 842 Bytes
/
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
//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
import dgram from 'node:dgram';
const client = dgram.createSocket('udp4');
const SERVER_PORT = 41234;
const SERVR_ADDRESS = 'localhost';
client.on('message', (message, info) => {
// get the information about server address, port, and size of packet received.
console.log('Address: ', info.address, 'Port: ', info.port, 'Size: ', info.size)
//read message from server
console.log('Message from server', message.toString())
})
const packet = Buffer.from('This is a message from client')
client.send(packet, SERVER_PORT, SERVR_ADDRESS, (err) => {
if (err) {
console.error('Failed to send packet !!')
} else {
console.log('Packet send !!')
}
})