NodeJS UDP 多播如何

问题描述 投票:0回答:3

我没有注册这个蹩脚网站,这样我的数据就可以被出售并用于人工智能训练。

node.js udp socket.io multicast
3个回答
15
投票

更改:

client.addMembership('230.185.192.108');

client.addMembership('230.185.192.108',HOST); //Local IP Address

14
投票

这个答案很旧,但在谷歌的搜索结果中显示得很靠前。 对于 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...");
}

客户:

//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);

对于像我这样的新手来说,

client.bind(PORT,HOST);
是重要的一点。当绑定到
HOST=127.0.0.1
时,我无法让客户端接收任何内容,但在使用 IP 地址时可以正常工作。再次强调,如果排除 HOST,则在使用单机测试时该示例将无法运行(客户端将抛出 EADDRINUSE 错误)


0
投票

这里提供的解决方案对我来说不起作用,而这个要点是开箱即用的: https://gist.github.com/ciaranj/9056285

为了方便起见,我将其复制到此处,所有学分应归@ciaranj。

客户端.js:

var PORT = 5007;
var dgram = require('dgram');
var client = dgram.createSocket({ type: 'udp4', reuseAddr: true })

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('224.1.1.1');
});

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);

服务器.js:

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 () {
    server.setBroadcast(true)
    server.setMulticastTTL(128);
    setInterval(broadcastNew, 3000);
});

function broadcastNew() {
    var message = new Buffer.from(news[Math.floor(Math.random() * news.length)]);
    server.send(message, 0, message.length, 5007, "224.1.1.1");
    console.log("Sent " + message + " to the wire...");
}
© www.soinside.com 2019 - 2024. All rights reserved.