0

Problem:

I can't figure out how to correctly use Node.js and dgram to send a UDP packet to a specific IP address within the same LAN (not behind a NAT)


Attempted Solution

I found Send Broadcast datagram showcasing how to use dgram with Node.js to send out UDP broadcasts.

server.js

var PORT = 6024;
var BROADCAST_ADDR = "192.168.0.255"; // substitute here
var dgram = require('dgram');
var server = dgram.createSocket("udp4");

server.bind(function() {
    server.setBroadcast(true);
    setInterval(broadcastNew, 3000);
});

function broadcastNew() {
    var message = Buffer.from("Broadcast message!");
    server.send(message, 0, message.length, PORT, BROADCAST_ADDR, function(err) {
        if (err) {
            console.log(err);
        }
        console.log("Sent '" + message + "'");
    });
}

client.js

var PORT = 6024;
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.setBroadcast(true);
});

client.on('message', function (message, rinfo) {
    console.log('Message from: ' + rinfo.address + ':' + rinfo.port +' - ' + message);
});

client.bind(PORT);

This appears to work correctly as I can confirm that the packets are being sent by using Wireshark with a display filter of udp.port==6024.

I then tried to do BROADCAST_ADDR = 192.168.0.26. server.js is run on computer 1 with 192.168.0.25 and client.js is run on computer 2 with 192.168.0.26. server.js does not show any errors, but both wireshark and client.js doesn't detect anything server.js is sending out.

I then tried to do BROADCAST_ADDR = 192.168.0.25 (basically sending to own IP address), and ran the 2 files on the same computer. client.js successfully receives the messages from server.js, but wireshark doesn't detect anything, which is very confusing to me.


I also tried to change server.js to

var PORT = 6024;
var BROADCAST_ADDR = "192.168.0.25";
var dgram = require('dgram');
var server = dgram.createSocket("udp4");

server.bind(function() {
    //server.setBroadcast(true);
    server.connect(PORT, BROADCAST_ADDR);
    setInterval(broadcastNew, 3000);
});

function broadcastNew() {
    var message = Buffer.from("Broadcast message!");
    server.send(message, 0, message.length, /*PORT, BROADCAST_ADDR,*/ function(err) {
        if (err) {
            console.log(err);
        }
        console.log("Sent '" + message + "'");
    });
}

but this doesn't actually change the behaviour


Another similar question is How to send a UDP packet via dgram in Nodejs? If I take this question's accepted solution code and edit it into

var dgram = require('dgram');

var client = dgram.createSocket('udp4');

client.send('Hello World!',0, 12, 6024, '192.168.0.26');
client.send('Hello2World!',0, 12, 6024, '192.168.0.26');
client.send('Hello3World!',0, 12, 6024, '192.168.0.26', function(err, bytes) {
client.close();
});

(I changed the IP address to the ip address I want to send to instead of the loopback address, and changed the port to 6024 so I can continue using the same wireshark display filter.)

This still exhibits the same behaviour where Wireshark and the other computer cannot detect any messages server.js is sending out, but if I send them to my own IP address 192.168.0.25 client.js can detect it if run on my computer.


I am using Node.js version v16.13.2

Can anyone point me to a resource that explains how to do this? I've been searching around online for a long time and can only find examples on udp broadcasts and sending to the 127.0.0.1 loopback address. Thanks

  • Sending to an IP (this is what you ask in the title) is much much simpler - see linked answer.. Broadcasting is too complex be be used as an example for a simple sending. – Steffen Ullrich Jul 17 '22 at 11:23
  • Using that example and swapping out the ip address with `192.168.0.26` makes it stop working (wireshark doesn't detect any packets and the other computer can't receive anything either). I just now realized I forgot to add this into my attempted solutions section so I'll be making an edit now. Sorry for wasting your time – Eisverygoodletter Jul 17 '22 at 11:28

0 Answers0