2

I am trying to create a distributed informational system that use peer to peer architecture with multicasting communication.

First of all I started to implement multicasting communication. I never did it so I have some issues that is hard to understand why it is not working properly.

Here is what I have managed to do:

Server

var SRC_PORT = 6025;
var PORT = 6024;
var MULTICAST_ADDR = '239.255.255.250';
var dgram = require('dgram');
var server = dgram.createSocket("udp4");

server.bind(SRC_PORT, function () {
    setInterval(multicastNew, 4000);
});

function multicastNew() {
    var message = new Buffer("Multicast message!");
    server.send(message, 0, message.length, PORT , MULTICAST_ADDR, function () {
        console.log("Sent '" + message + "'");
    });
}

Client

var PORT = 6024;
var MULTICAST_ADDR = '239.255.255.250';
var dgram = require('dgram');
var client = dgram.createSocket('udp4');

client.on('listening', function () {
    client.setBroadcast(true);
    var address = client.address();
    console.log('UDP Client listening on ' + address.address + ":" + address.port);
});

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

client.bind(PORT, function () {
    client.addMembership(MULTICAST_ADDR);
});

When I start the server and the client everything works. The message from the server.js successful is send to client.js.

Problem

The problem appears when I want to have several clients (nodes). In future I would like to make a request to all clients(nodes) what mistake I am making that doesn't allow to send some information from all of them using multicasting.

Using the code above I am not able to open at the same time 2 clients to receive the message from the server on both of them.

Here is the error when trying to connect more then 1 client:

events.js:85
      throw er; // Unhandled 'error' event
            ^
Error: bind EADDRINUSE
    at exports._errnoException (util.js:746:11)
    at dgram.js:224:28
    at dns.js:85:18
    at process._tickCallback (node.js:355:11)
    at Function.Module.runMain (module.js:503:11)
    at startup (node.js:129:16)
    at node.js:814:3

I understand that the problem is that I am using the same code twice (client.js) with the same ports and other parameters. What mistake I am making in order to send (via server.js) and receive successful messages using multicasting on more clients?

Chris
  • 884
  • 2
  • 11
  • 22
  • Use a different computer. Or a different interface, yet still listening on the same network. One of the clients can be listening on your Wireless network and the other one on your Wired connection. This way you can bind to the same port from different IP addresses and do the broadcast from a single computer. – Itay Grudev Oct 30 '15 at 17:51
  • @ItayGrudev Thank you for hint, it really works if I use 2 computers on the same wifi. But I need to simulate on one machine what can I do? – Chris Oct 30 '15 at 18:10
  • I told you, you can use both your wired connection and your Wifi connection and bind to their corresponding IP addresses. – Itay Grudev Oct 30 '15 at 18:13
  • @ItayGrudev but in case I need to have 7 clients (nodes) It will not work. – Chris Oct 30 '15 at 18:16
  • I won't, but if it works with 2 it will work with 7, right? :D – Itay Grudev Oct 30 '15 at 23:45
  • This is more of a networking / technology than a programming issue. Try asking it on [SuperUser.com](http://superuser.com) – Itay Grudev Oct 30 '15 at 23:46

2 Answers2

1

I am not sure, which OS you are on, but you could create 7 virtual interfaces on that host with different IP's and use unique IP for each of your clients.

That way all the clients could bind to same port and talk to Server

HTH

VenkatC
  • 602
  • 3
  • 5
  • ok, you could use the procedure outlined here http://blog.fabiomorais.com.br/2011/05/how-to-configure-virtual-network.html with additional static ip's – VenkatC Nov 07 '15 at 02:42
0

To allow multiple multicast clients to receive messages on the same port you need to set the SO_REUSEADDR and IP_MULTICAST_LOOP socket options.

the8472
  • 40,999
  • 5
  • 70
  • 122