Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A *working* (on Windows) UDP Multicast client & server with Node.Js v0.10.25 (I spent a *LOT* of time getting EINVAL and I still don't quite know why :/)
For my own sanity ;) Scraped from a variety of places, including:
var news = [
"Borussia Dortmund wins German championship",
"Tornado warning for the Bay Area",
"More rain for the weekend",
"Android tablets take over the world",
"iPad2 sold out",
"Nation's rappers down to last two samples"
var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind( function() {
setInterval(broadcastNew, 3000);
function broadcastNew() {
var message = new Buffer(news[Math.floor(Math.random()*news.length)]);
server.send(message, 0, message.length, 5007, "");
console.log("Sent " + message + " to the wire...");
var PORT = 5007 ;
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) {
console.log('A: Epic Command Received. Preparing Relay.');
console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + message);
Copy link

TTatum commented Oct 22, 2014

I've been trying to do this for days! This is the first working model I've seen. Thank you!!!

Copy link

willemmulder commented Nov 10, 2014

EINVAL indicates that the socket already has an address (see Maybe you tried to bind twice?

Copy link

jsammarco commented Nov 27, 2017

Thank you for this but how can the client send data back to the server?

Copy link

dirkk0 commented Dec 5, 2022

still works right out of the box! Thank you!

The only things I changed was

  • var client = dgram.createSocket({ type: 'udp4', reuseAddr: true }) in the client and
  • var message = new Buffer.from(news[Math.floor(Math.random() * news.length)]) in the server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment