多播ip地址-在对recvfrom的调用中被阻止 [英] multicast ip address - blocked in call to 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 这是我从书中看到的.我认为我们可以在进行必需的setsockopt调用时使用任何D类地址.
在端口1025上连接
运行linux内核2.6.25
还要如何检查多播支持是否已编译到内核中.
更新:
我确实在shell上路由添加-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.
ok i disabled the firewall and i could get the program running. got help from here
now more problems:
- how to add rules to the firewall to specifically my code to run
- if i do not disable loopback, then i do not get the messages from another process sending on the same multicast group, and if i enable it then my process receives the messages it transmits. this is because i am running both the programs on localhost. is there a way around.
这篇关于多播ip地址-在对recvfrom的调用中被阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!