UDP: Listening to the same port for two different multicast streams

前端 未结 4 756
无人及你
无人及你 2021-01-01 03:16

I need to listen to 2 different multicast groups using the same port. Program A will listen from 230.0.0.1 and Program B from 23

4条回答
  •  别那么骄傲
    2021-01-01 03:54

    (from answer of Receving multiple multicast feeds on the same port - C, Linux)

    The ip(7) manpage describe a possible solution :

    IP_MULTICAST_ALL (since Linux 2.6.31)
    This option can be used to modify the delivery policy of multicast messages to sockets bound to the wildcard INADDR_ANY address. The argument is a boolean integer (defaults to 1). If set to 1, the socket will receive messages from all the groups that have been joined globally on the whole system. Otherwise, it will deliver messages only from the groups that have been explicitly joined (for example via the IP_ADD_MEMBERSHIP option) on this particular socket.

    Then you can activate the filter to receive messages of joined groups using :

    int mc_all = 0;
    if ((setsockopt(sock, IPPROTO_IP, IP_MULTICAST_ALL, (void*) &mc_all, sizeof(mc_all))) < 0) {
        perror("setsockopt() failed");
    }
    

提交回复
热议问题