Created
May 8, 2012 08:41
-
-
Save sixtus/2633625 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Notice that neither can receive any message on sender. It stops receiving messages, when I pass an IP address to bind(). | |
However, I am required to pass an IP address to bind() to make the Mac use it's non-default gw. | |
So here, I am also showing my work around, which is having two sockets with different configuration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is Linux, 192.168.178.45, on default inet. | |
RECEIVER received from 192.168.178.22 : [sender] pid: 32651 | |
RECEIVER received from 192.168.178.45 : [sender] pid: 1996 | |
RECEIVER received from 192.168.178.45 : [receiver] pid: 1996 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dgram = require('dgram'); | |
var sender = dgram.createSocket('udp4'); | |
var receiver = dgram.createSocket('udp4'); | |
var testSender = new Buffer('[sender] pid: ' + process.pid); | |
var testReceiver = new Buffer('[receiver] pid: ' + process.pid); | |
var multicastAddress = '239.1.2.3'; | |
var multicastPort = 5554; | |
var wlan = require('os').networkInterfaces().wlan0; | |
// Mac interface is en1 | |
if (!wlan) { | |
wlan = require('os').networkInterfaces().en1; | |
} | |
if (wlan) { | |
wlan = wlan.filter(function(inet) { return inet.family === 'IPv4' })[0]; | |
} | |
sender.bind(multicastPort, wlan.address); | |
sender.addMembership(multicastAddress, wlan.address); | |
receiver.bind(multicastPort); | |
receiver.addMembership(multicastAddress, wlan.address); | |
receiver.on('message', function ( data, rinfo ) { | |
console.log('RECEIVER received from ', rinfo.address, ' : ', data.toString()); | |
}); | |
sender.on('message', function ( data, rinfo ) { | |
console.log('SENDER received from ', rinfo.address, ' : ', data.toString()); | |
}); | |
setInterval(function () { | |
receiver.send(testReceiver, 0, testReceiver.length, multicastPort, multicastAddress); | |
sender.send(testSender, 0, testSender.length, multicastPort, multicastAddress); | |
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a Mac, 192.168.178.22, where this is the non-default inet: | |
RECEIVER received from 192.168.178.22 : [sender] pid: 32651 | |
RECEIVER received from 192.168.178.45 : [sender] pid: 1996 | |
RECEIVER received from 192.168.178.45 : [receiver] pid: 1996 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And here is the output, when the Mac is on default gw (same output for both machines again):
RECEIVER received from 192.168.178.22 : [sender] pid: 32735
RECEIVER received from 192.168.178.22 : [receiver] pid: 32735
RECEIVER received from 192.168.178.45 : [sender] pid: 2002
RECEIVER received from 192.168.178.45 : [receiver] pid: 2002