-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdpClient.js
37 lines (29 loc) · 1.04 KB
/
UdpClient.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
const util = require('util');
var ListenPort = 44444;
var SendPort = 11111;
var ServerIp = '172.24.71.214';
var dgram = require('dgram');
var client = dgram.createSocket('udp4');
client.on('listening', function() {
var address = client.address();
console.log('UDP client listening on ' + address.address + ':' + address.port);
});
client.on('message', function(message, remote) {
//var data = message.readFloatLE(0);
//console.log(remote.address + ':' + remote.port +' - ' + message + "\tdate:" + data);
//var datebuf = Buffer.allocUnsafe(4);
//datebuf.writeFloatLE(Date.now(), 0);
sendmsg = JSON.stringify({ name: "rokoko", arrival_time: Date.now() });
var data = Buffer.from(sendmsg);
console.log("client send\t" + util.inspect(sendmsg, {showHidden: false, depth: null}));
client.send(data, 0, data.length, SendPort, ServerIp, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + ServerIp +':'+ SendPort);
client.close();
});
});
client.bind({
address: ServerIp,
port: ListenPort,
exclusive: true
});