UDP Client Cannot send message
I have implemented a Java-based UDP Server to listen on UDP messages over the port 5172. I am trying to communicate to the server from the Node.js client by sending the UDP Message.
const client = dgram.createSocket('udp4');
const buf1 = Buffer.from('Test');
client.send(buf1,0,buf1.length, 5172, '127.0.0.1', (err) => {
client.close();
});
When I execute the code, I get the error ERR_SOCKET_DGRAM_NOT_RUNNING.
Extending this I have tested the UDP Server by sending messages from Packet Sender. With this, I could not only send the message but also receive it on the server.
Can you please help me in this regard?
Can you post a complete example that demonstrates the problem? As well, can you post the stack trace you get?
Thank you @bnoordhuis for the quick response! Herewith, I have attached my code and the stack trace. (I am relatively new to node.js Sorry if I miss something that is very simple)
I am trying to get the content of the URL http://icanhazip.com/ and forward it as a UDP message to the server on port 5172.
const dgram = require('dgram');
const request = require('request');
const client = dgram.createSocket('udp4');
var getIP = function() {
console.log("Sending request.")
request({
url: "http://icanhazip.com/",
method: "GET",
timeout: 10000,
followRedirect: true,
maxRedirects: 10
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(response.body);
var buf1 = Buffer.from(response.body);
client.send(buf1, 0, buf1.length, 5172, '127.0.0.1', (err) => {
client.close();
});
} else {
console.log('error' + response.statusCode);
}
});
}
getIP(); // Do on start
var requestLoop = setInterval(getIP, 1000);
Here is the error I obtain:

@anandbaskaran : Your node.js client code is closing the UDP socket connection through which you are sending your server the response (request made to http://icanhazip.com) immediately after the first response is received as part of your getIP() processing.
You are executing the getIP() loop at 1 second intervals. You conduct the GET request against http://icanhazip.com at that frequency, and the two "Sending request." messages are the successful results of those first two requests; however, you closed the UDP socket connection after the first response, and so your UDP socket connection is no longer available to process the second response (send the response buffer to your server), hence:
Error [ERR_SOCKET_DGRAM_NOT_RUNNING]: Not running
Its very easy to demonstrate this. For example, add the following listener on your client's UDP socket:
client.on('close' function() {
console.log('Client UDP socket closed : BYE!')
});
And, as a simple fix to cause this client to continue indefinitely (if this is your intention), comment out/remove your client.close(); call and then it will repeatedly query that URL, and send the response to your other server.
Perfect!! Thanks a lot for helping!