I’m trying to receive a multicast data from specific network interface on CentOS 5.5
sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sd, (sockaddr*)&addr, sizeof(sockaddr_in));
setsockopt(sd, SOL_SOCKET, SO_BINDTODEVICE, "eth0", 5);
But I’m receiving packets from all interfaces.
What wrong?
First, check if any of your calls fail, socket,bind,setsockopt in this case. Printing an error message with the perror() function will help you diagnose problems.
However, for receiving multicast datagrams you might need to specify the ip address of the interface when you join a multicast group using the IP_ADD_MEMBERSHIP socket option
Something like
where the
mreqstruct isMore info here.