NodeJS UDP 多播如何
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14130560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NodeJS UDP Multicast How to
提问by Taurian
I'm trying to send a UDP Multicast Packet to: 230.185.192.108 so everyone subscribed will receive. A bit stuck. I believe it's broadcasting correctly, but can't seem to pick anything up with any client.
我正在尝试将 UDP 多播数据包发送到:230.185.192.108,以便每个订阅的人都会收到。有点卡住了。我相信它的广播是正确的,但似乎无法从任何客户端获取任何信息。
Server:
服务器:
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();
server.setBroadcast(true)
server.setMulticastTTL(128);
server.addMembership('230.185.192.108');
setInterval(broadcastNew, 3000);
function broadcastNew() {
var message = new Buffer(news[Math.floor(Math.random()*news.length)]);
server.send(message, 0, message.length, 8088, "230.185.192.108");
console.log("Sent " + message + " to the wire...");
//server.close();
}
Client
客户
var PORT = 8088;
var HOST = '192.168.0.102';
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.setBroadcast(true)
client.setMulticastTTL(128);
client.addMembership('230.185.192.108');
});
client.on('message', function (message, remote) {
console.log('A: Epic Command Received. Preparing Relay.');
console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + message);
});
client.bind(PORT, HOST);
ReferencesMore info on NodeJS Datagram
参考资料更多关于 NodeJS 数据报的信息
采纳答案by Taurian
Changed:
更改:
client.addMembership('230.185.192.108');
to
到
client.addMembership('230.185.192.108',HOST); //Local IP Address
回答by Abhijit Karnik
This answer is old, but shows up high on Google's search results.
With Node v4.4.3, the server example fails with error EBADF. The complete working block of code is listed below:
Server:
这个答案很旧,但在谷歌的搜索结果中显示得很高。使用 Node v4.4.3,服务器示例失败并显示错误 EBADF。下面列出了完整的工作代码块:
服务器:
//Multicast Server sending messages
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 PORT = 41848;
var MCAST_ADDR = "230.185.192.108"; //not your IP and should be a Class D address, see http://www.iana.org/assignments/multicast-addresses/multicast-addresses.xhtml
var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind(PORT, function(){
server.setBroadcast(true);
server.setMulticastTTL(128);
server.addMembership(MCAST_ADDR);
});
setInterval(broadcastNew, 3000);
function broadcastNew() {
var message = new Buffer(news[Math.floor(Math.random()*news.length)]);
server.send(message, 0, message.length, PORT,MCAST_ADDR);
console.log("Sent " + message + " to the wire...");
}
Client:
客户:
//Multicast Client receiving sent messages
var PORT = 41848;
var MCAST_ADDR = "230.185.192.108"; //same mcast address as Server
var HOST = '192.168.1.9'; //this is your own IP
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.setBroadcast(true)
client.setMulticastTTL(128);
client.addMembership(MCAST_ADDR);
});
client.on('message', function (message, remote) {
console.log('MCast Msg: From: ' + remote.address + ':' + remote.port +' - ' + message);
});
client.bind(PORT, HOST);
For the novices like me, client.bind(PORT,HOST);is the important bit. I couldn't get the client to receive anything when bound to HOST=127.0.0.1, but worked when the IP address was used. Again, HOST if excluded, the example won't work when testing using a single machine (client will throw EADDRINUSE error)
对于像我这样的新手来说,client.bind(PORT,HOST);是重要的一点。绑定到 时HOST=127.0.0.1,我无法让客户端接收任何内容,但是在使用 IP 地址时却可以工作。同样,如果排除 HOST,则在使用单台机器进行测试时该示例将不起作用(客户端将抛出 EADDRINUSE 错误)

