多播 IP 地址 - 在调用 recvfrom 时被阻止
我正在编写一个简单的多播应用程序。我打算在本地主机上运行它。
我已完成以下操作:
char *maddr;
.
.
.
sendfd = socket(...);
struct sockaddr_in sasend;
sasend.sin_family = AF_INET;
sasend.sin_port = htonl(portno);
inet_ntop(maddr, &(sasend.sin_addr.s_addr));
struct sockaddr_in sarecv;
memcpy(&sarecv, &sasend);
recvfd = socket(...);
const int on = 1;
setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); // can you explain why
// this is needed
bind(recvfd, &sarecv);
struct ip_mreq mreq;
memcpy(&mreq.imr_multiaddr, &(sasend.sin_addr));
mreq.imr_interface = htonl(INADDR_ANY);
setsockopt(recvfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
char flag = 1;
setsockopt(sendfd, IPPROTO_IP, IP_MULTICAST_LOOP, &flag, 1);
if (fork() == 0) {
while (recvfrom(recvfd)) {
}
}
else {
while (sendto(sendfd)) {
sleep(3);
}
}
在实际代码中,我正在检查所有系统调用的返回值。 问题是recvfrom不返回。该进程在对 recvfrom 的调用中保持阻塞状态。
我尝试在不同的 shell 上运行该程序的两个实例。这没有帮助。
我也尝试将环回标志设置为 0,但没有帮助。
我想从本地主机运行这两个程序。
我使用的多播地址是239.255.1.2 这是我从书中看到的。我认为我们可以使用任何 D 类地址,因为我们正在进行所需的 setsockopt 调用。
连接到
运行 linux 内核 2.6.25 的
端口 1025还如何检查多播支持是否已编译到内核中。
更新:
我在 shell 上执行了route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0。 问题仍然存在。
i am writing a simple multicast application. i intend to run it on localhost.
i have done the following:
char *maddr;
.
.
.
sendfd = socket(...);
struct sockaddr_in sasend;
sasend.sin_family = AF_INET;
sasend.sin_port = htonl(portno);
inet_ntop(maddr, &(sasend.sin_addr.s_addr));
struct sockaddr_in sarecv;
memcpy(&sarecv, &sasend);
recvfd = socket(...);
const int on = 1;
setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); // can you explain why
// this is needed
bind(recvfd, &sarecv);
struct ip_mreq mreq;
memcpy(&mreq.imr_multiaddr, &(sasend.sin_addr));
mreq.imr_interface = htonl(INADDR_ANY);
setsockopt(recvfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
char flag = 1;
setsockopt(sendfd, IPPROTO_IP, IP_MULTICAST_LOOP, &flag, 1);
if (fork() == 0) {
while (recvfrom(recvfd)) {
}
}
else {
while (sendto(sendfd)) {
sleep(3);
}
}
in the actual code i am checking the return values of all system calls.
the problem is that recvfrom does not return. the process stays blocked in the call to recvfrom.
i have tried running two instances of the program on different shells. it does not help.
also i tried setting loopback flag to 0, it does not help.
i want to run both the programs from localhost.
the multicast address i am using is 239.255.1.2
which i have seen from the book. i think we can use any class D address as we are making the required setsockopt call.
connecting on port 1025
running linux kernel 2.6.25
also how do i check if multicasting support has been compiled into the kernel.
Update:
i did route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 on the shell.
still the problem exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我禁用了防火墙,我可以让程序运行。
从这里获得帮助
现在更多问题:
ok i disabled the firewall and i could get the program running.
got help from here
now more problems: