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?