-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp-client.js
37 lines (29 loc) · 841 Bytes
/
udp-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
31
32
33
34
35
36
37
var udp = require('dgram');
// -------------------- udp client ----------------
var buffer = require('buffer');
// creating a client socket
var client = udp.createSocket('udp4');
//buffer msg
var data = Buffer.from('siddheshrane');
client.on('message', function (msg, info) {
console.log('Data received from server : ' + msg.toString());
console.log('Received %d bytes from %s:%d\n', msg.length, info.address, info.port);
});
//sending msg
client.send(data, 2222, 'localhost', function (error) {
if (error) {
client.close();
} else {
console.log('Data sent !!!');
}
});
var data1 = Buffer.from('hello');
var data2 = Buffer.from('world');
//sending multiple msg
client.send([data1, data2], 2222, 'localhost', function (error) {
if (error) {
client.close();
} else {
console.log('Data sent !!!');
}
});