我在UDP多播概念证明上反复出现EADDRNOTAVAIL问题。我有一个Linux服务器,它将多播消息发送到Windows客户端(尝试将其反转无济于事。)。当客户端和服务器在同一台计算机上但不在同一网络上运行时,服务器可以正常工作。

以下是我尝试使其使用的代码,正在使用的IP以及收到的错误。

我不确定自己做错了什么!



环境

Windows本地IP:192.168.1.10
Linux本地IP:192.168.1.11

服务器:server.js

const SRC_PORT = 6025;
const PORT = 6024;
const MULTICAST_ADDR = '239.255.255.250';
const LOCAL_IP = '192.168.1.11';
const dgram = require('dgram');
const server = dgram.createSocket("udp4");

server.bind(SRC_PORT, LOCAL_IP, function () {
  setInterval(function () {
    let message = new Buffer(new Date().toLocaleTimeString());
    server.send(message, 0, message.length, PORT, MULTICAST_ADDR, function () {
      console.log("Sent '" + message + "'");
    });
  }, 4000);
});




客户:client.js

const PORT = 6024;
const MULTICAST_ADDR = '239.255.255.250';
const LOCAL_IP = '192.168.1.10';
const HOST_IP = '192.168.1.11';
const dgram = require('dgram');
const client = dgram.createSocket('udp4');

client.on('listening', function () {
  let address = client.address();
  console.log('UDP Client listening on ' + address.address + ":" + address.port);
});

client.on('message', function (message, rinfo) {
  console.log('Message from: ' + rinfo.address + ':' + rinfo.port + ' - ' + message);
});

//*** Also tried with client.bind(PORT, LOCAL_IP, ... and failed
client.bind(PORT, function () {
  client.addMembership(MULTICAST_ADDR, HOST_IP);
});


错误

dgram.js:508
    throw errnoException(err, 'addMembership');
    ^

Error: addMembership EADDRNOTAVAIL
    at exports._errnoException (util.js:1026:11)
    at Socket.addMembership (dgram.js:508:11)
    at Socket.<anonymous> (~/dev/node-multicast/client.js:16:10)
    at Socket.g (events.js:286:16)
    at emitNone (events.js:91:20)
    at Socket.emit (events.js:185:7)
    at startListening (dgram.js:121:10)
    at dgram.js:228:7
    at _combinedTickCallback (internal/process/next_tick.js:77:11)

最佳答案

我确定您从此错误中加入了错误的multicastInterface:


  addMembership EADDRNOTAVAIL


它应该是您的本地接口地址:
client.addMembership(MULTICAST_ADDR, LOCAL_IP);

正如doc所说:


  socket.addMembership(multicastAddress[, multicastInterface])
  告诉内核加入给定的组播组
  使用IP_ADD_MEMBERSHIP的multicastAddress和multicastInterface
  套接字选项。如果未指定multicastInterface参数,
  操作系统将选择一个界面并添加成员资格
  对它。要将成员资格添加到每个可用接口,请致电
  addMembership多次,每个接口一次。

07-26 05:29