以下代码仅在我的子网中接收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. */

最佳答案

您需要通过所有接口(而不只是192.168.3.2)加入该组。

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

关于c - 如何从所有子网接收UDP多播?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17612457/

10-11 19:57