首页 文章

NodeJS UDP组播如何

提问于
浏览
23

我正在尝试将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);

References 有关NodeJS数据报的更多信息

2 回答

  • 9

    Changed:

    client.addMembership('230.185.192.108');
    

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

    这个答案很老,但在Google的搜索结果中显得很高 . 使用Node v4.4.3,服务器示例失败,错误为EBADF . 完整的代码工作块如下:

    Server:

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

    对于像我这样的新手来说, client.bind(PORT,HOST); 是重要的一点 . 当绑定到 HOST=127.0.0.1 时,我无法让客户端接收任何内容,但在使用IP地址时工作 . 再次,HOST如果被排除,该示例在使用单个机器进行测试时将无法工作(客户端将抛出EADDRINUSE错误)

相关问题