English 中文(简体)
multicast ip address - blocked in call to recvfrom
原标题:

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.
问题回答

暂无回答




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签