本文介绍了在使用多个NIC侦听C ++中的多播时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好

我试图在一台有两个NIC的机器上写一个多播客户端,但我无法使它工作。

我可以看到一个嗅探器,一次我启动程序NIC(eth4)开始接收多播数据报,但是,我不能收到()我的程序中的任何。

当运行tshark -i eth4 -R udp.port == xxx(multicast port)

我得到:

1059.435483 yyyy (一些ip) - > zzzz(多播ip,而不是我的eth4网卡IP)UDP源端口:kkk(一些其他端口)目标端口:xxx(多播端口)

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;
}   


推荐答案

(代码更新)。

我也添加到路由表route add -net 224.0.0.0 netmask 240.0.0.0 dev eth4

仍然,没有成功。

除了我之外,我还需要在我的环境中设置什么?

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?

这篇关于在使用多个NIC侦听C ++中的多播时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 04:45