我遇到了关于UDP多播概念证明的重复#include <stdio.h>
enum { NMLIM = 5, BUFSIZE = 256 };
int main (void) {
char buf[BUFSIZE] = "";
while (fgets (buf, BUFSIZE, stdin)) {
char *p = buf, cmd, /* start & end pointers */
fname[NMLIM+1] = "",
sname[NMLIM+1] = "";
size_t n = 0;
/* validate first char is 'a-z' and followed by ' ' */
if (*p && 'a' <= *p && *p <= 'z' && *(p + 1) == ' ') {
cmd = *p;
p += 2; /* advance pointer to next char following ' ' */
}
else { /* handle error */
fprintf (stderr, "error: no single command followed by space.\n");
return 1;
}
/* read up to NLIM chars into fname */
for (n = 0; n < NMLIM && *p && *p != ' ' && *p != '\n'; p++)
fname[n++] = *p;
fname[n] = 0; /* nul terminate */
/* discard remaining chars up to next ' ' */
while (*p && *p != ' ') p++;
p++; /* advance to next char */
/* read up to NLIM chars into sname */
for (n = 0; n < NMLIM && *p && *p != ' ' && *p != '\n'; p++)
sname[n++] = *p;
sname[n] = 0; /* nul terminate */
printf ("input : %soutput : %c %s %s\n",
buf, cmd, fname, sname);
}
return 0;
}
问题的问题。我有一台Linux服务器向Windows客户端发送多播消息(尝试将其反转为无效......)。在同一台计算机上运行两者客户端和服务器时,服务器可以正常工作,但不能通过相同的网络运行。
下面是我尝试使用的代码,我正在使用的IP以及我收到的错误。
我不确定我做错了什么!
Windows本地IP:$ echo "a bbbbbbbb cc" | ./bin/walkptr
input : a bbbbbbbb cc
output : a bbbbb cc
Linux本地IP:EADDRNOTAVAIL
192.168.1.10
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
答案 0 :(得分:1)
我确定你从这个错误中加入了错误的multicastInterface:
addMembership EADDRNOTAVAIL
它应该是您本地接口的地址:
client.addMembership(MULTICAST_ADDR, LOCAL_IP);
正如doc所说:
socket.addMembership(multicastAddress[, multicastInterface])
告诉内核加入给定的组播组 multicastAddress和multicastInterface使用IP_ADD_MEMBERSHIP 套接字选项。如果未指定multicastInterface参数, 操作系统将选择一个界面并添加成员资格 它。要为每个可用接口添加成员资格,请致电 addMembership多次,每个接口一次。