I am trying to write an app in node.js to send a udp request to a device ( which has udp server) and then receive the response and display it. The device acts in such a way that upon receipt of a request on its port 11220 it returns a response immediately.
The code below sends a udp request to the device and the device responses back immediately ( I can see it in wireshark) but I can not handle/display the revived message. Or at least I just want to be able to display a message upon receipt of a reply from the device. Please let me know what is missing in my code or show me a complete code to do it. Very much appreciated.
As well, I do not prefer to use socket.io etc.
var PORT = 11220 ;
var HOST = '192.168.1.111';
var dgram = require('dgram');
var message = new Buffer(9);
var client = dgram.createSocket('udp4');
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
client.close();
});
client.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ":" + address.port);
});
client.on('message', function (message, remote) {
// CAN I HANDLE THE RECIVED REPLY HERE????
console.log(remote.address + ':' + remote.port +' - ' + message);
});
message
handler is never run? Does thelistening
one run? What happens if yousend
only after adding the event handlers. – Eunuchoidism