Problem in listening to multicast in C++ with multiple NICs

耗尽温柔 提交于 2019-12-08 12:24:58

问题


Hi
I am trying to write a multicast client on a machine with two NICs, and I can't make it work.
I can see with a sniffer that once I start the program the NIC (eth4) start receiving the multicast datagrams, However, I can't recieve() any in my program.
when running "tshark -i eth4 -R udp.port==xxx (multicast port)"
I get:
1059.435483 y.y.y.y. (some ip) -> z.z.z.z (multicast ip, not my eth4 NIC IP) UDP Source port: kkk (some other port) Destination port: xxx (multicast port)

Searched the web for some examples/explanations, but it seems like I do what everybody else does.
Any help will be appreciated...
Thanks!

bool connectionManager::sendMulticastJoinRequest()
{
    struct sockaddr_in localSock;
    struct ip_mreqn group;

    char* mc_addr_str = SystemManager::Instance()->getTCP_IP_CHT();
    char* local_addr_str = SystemManager::Instance()->getlocal_IP_TOLA();
    int port = SystemManager::Instance()->getTCP_Port_CHT();
    /* Create a datagram socket on which to receive. */

    CHT_UDP_Feed_sock = socket(AF_INET, SOCK_DGRAM, 0);

    if(CHT_UDP_Feed_sock < 0)
    {
        perror("Opening datagram socket error");
        return false;
    }
    /* application to receive copies of the multicast datagrams. */
    {
      int reuse = 1;
      if(setsockopt(CHT_UDP_Feed_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
      {
          perror("Setting SO_REUSEADDR error");
          close(CHT_UDP_Feed_sock);
          return false;
      }
    }

    /* Bind to the proper port number with the IP address */
    /* specified as INADDR_ANY. */
    memset((char *) &localSock, 0, sizeof(localSock));
    localSock.sin_family = AF_INET;
    localSock.sin_port = htons(port);
    localSock.sin_addr.s_addr =inet_addr(local_addr_str); // htonl(INADDR_ANY); //
    if(bind(CHT_UDP_Feed_sock, (struct sockaddr*)&localSock, sizeof(localSock)))
    {
        perror("Binding datagram socket error");
        close(CHT_UDP_Feed_sock);
        return false;
    }

    /* Join the multicast group mc_addr_str on the local local_addr_str */
    /* interface. Note that this IP_ADD_MEMBERSHIP option must be */
    /* called for each local interface over which the multicast */
    /* datagrams are to be received. */

    group.imr_ifindex = if_nametoindex("eth4");
    if (setsockopt(CHT_UDP_Feed_sock, SOL_SOCKET, SO_BINDTODEVICE, "eth4", 5) < 0)
        return false;
    group.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
    group.imr_address.s_addr = htonl(INADDR_ANY); //also tried inet_addr(local_addr_str); instead
    if(setsockopt(CHT_UDP_Feed_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
    {
        perror("Adding multicast group error");
        close(CHT_UDP_Feed_sock);
        return false;
    }

    // Read from the socket.
    char databuf[1024];
    int datalen = sizeof(databuf);
    if(read(CHT_UDP_Feed_sock, databuf, datalen) < 0)
    {
        perror("Reading datagram message error");
        close(CHT_UDP_Feed_sock);
        return false;
    }
    else
    {
        printf("Reading datagram message...OK.\n");
        printf("The message from multicast server is: \"%s\"\n", databuf);
    }
    return true;
}   

回答1:


Before the call to IP_ADD_MEMBERSHIP set up group like this:

group.imr_multiaddr.s_addr = inet_addr(mc_addr_str);
group.imr_address.s_addr = inet_addr(local_addr_str);

You can leave the call to SO_BINDTODEVICE in before that, but that's only necessary if you would only like to use one specific interface.




回答2:


I set up the group exactly like that (code is updated).
I also add to the routing table "route add -net 224.0.0.0 netmask 240.0.0.0 dev eth4"
still, with no success.
anything else I need to set up in my environment besides that?



来源:https://stackoverflow.com/questions/5111209/problem-in-listening-to-multicast-in-c-with-multiple-nics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!