我如何从所有子网接收UDP多播?

时间:2013-07-12 10:07:14

标签: c linux sockets udp multicast

以下代码仅在我的子网中接收UDP多播。 我如何从所有子网接收UDP多播?

    sd = socket(AF_INET, SOCK_DGRAM, 0);
    int reuse = 1;
    setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
            (char *) &reuse, sizeof(reuse));

    memset((char *) &localSock, 0, sizeof(localSock));
    localSock.sin_family = AF_INET;
    localSock.sin_port = htons(1900);
    localSock.sin_addr.s_addr = INADDR_BROADCAST;

    bind(sd, (struct sockaddr*) &localSock, sizeof(localSock));

    group.imr_multiaddr.s_addr = inet_addr("239.255.255.250");
    group.imr_interface.s_addr = inet_addr("192.168.3.2");

    setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
            (char *) &group, sizeof(group));


    /* Read from the socket. */

1 个答案:

答案 0 :(得分:0)

您需要通过所有接口加入该组,而不仅仅是192.168.3.2。

您还需要将套接字绑定到INADDR_ANY,而不是INADDR_BROADCAST。