gpt4 book ai didi

node.js - NodeJS UDP 多播如何

转载 作者:IT老高 更新时间:2023-10-28 22:02:22 27 4
gpt4 key购买 nike

我正在尝试将 UDP 多播数据包发送到:230.185.192.108,以便订阅的每个人都能收到。有点卡住了。我相信它可以正确广播,但似乎无法从任何客户端获取任何内容。

服务器:

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

客户

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

引用资料更多关于 NodeJS 数据报的信息

最佳答案

已更改:

client.addMembership('230.185.192.108');

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

关于node.js - NodeJS UDP 多播如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14130560/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com