IP multicasting is used to target a group of hosts by sending a single datagram. IP addresses in the range 224.0.0.0 through 239.255.255.255 are reserved for multicasting.
To find out which hosts on your subnet support multicasting, try
ping 224.0.0.1
Here’s a Node.js code snippet that sends UDP datagrams to multicast group 225.0.0.1 at port 8001
var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.bind(8000);
var b = new Buffer("Hello");
s.send(b, 0, b.length, 8001, "225.0.0.1", function(err, bytes) {
console.log("Sent " + bytes + " bytes");
s.close();
});
A host that desires to receive a datagram sent to a multicast group, must first request membership to that group. Here’s a Node.js code snippet that receives datagram sent by the code above
var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.bind(8001, function() {
s.addMembership('225.0.0.1');
});
s.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " +
rinfo.address + ":" + rinfo.port);
});
.NET code that does something similar can be found in the UDP Tool at GitHUb.
Receiving multicasts on Linux does not work when you bind the socket to a specific interface, for instance s.bind(8001, 192.168.1.1… does not work. It looks like a Linux-only quirk because it happens with both the Mono .NET runtime and Node.js.
Another quirk observed on Linux is the need to add a route to forward multicast IP packets to a wireless LAN interface that is used in access point mode, in tandem with hostapd e.g.
route add -net 225.0.0.0 netmask 255.0.0.0 gw 192.168.1.1
Filed under: .NET, JavaScript, Network








