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

前端 未结 4 755
无人及你
无人及你 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:47

    The problem with socket code is that "recvfrom" will only give you the source address from where the packet was sent from. It doesn't tell you the IP address of where the packet was sent to. You want to be able to inspect the destination address of the UDP packet so you can filter out packets that were sent to multicast IP addresses you are not interested in.

    There is a socket option you can set followed by the use of "recvmsg" instead of recv or recvfrom to get the destination IP affffdress the packet was sent to.

    1) Use setsockopt with IP_PKTINFO to enable getting the destination IP address passed up to the app level for data received on the socket.

    int enable = 1;
    setsockopt(sock, IPPROTO_IP , IP_PKTINFO , &enable, sizeof(enable));
    

    2) Use recvmsg instead of recvfrom (or recv) to get the destination address the UDP packet was sent to. I have a helper function called "recvfromex" that wraps recvmsg and mirrors the functionality of recvfrom - expect it has an extra parameter for the caller to get the destination IP of the packet.

    It's a bit windy to post - but you can look at my C++ code from my github project and take what you need.

    Look at the recvfromex function here

    More code sample for the setsockopt call here (look for the function "EnablePktInfo" on how to use the setsockopt call with IP_PKTINFO). Also contains extensions for IPV6 and BSD.

    0 讨论(0)
  • 2021-01-01 03:54

    "connect" might be what you need after all. Typically, for connecting TCP sockets, the man page also suggests that it can be used for filtering out UDP packets from other addresses:

    From the man page posted here:

    If the socket sockfd is of type SOCK_DGRAM then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received.

    0 讨论(0)
  • 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");
    }
    
    0 讨论(0)
  • 2021-01-01 03:56

    If you change

    in_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    

    to

    inet_aton(<your wanted IP address>, &in_addr.sin_addr.s_addr);
    

    you could have more success.

    (And if you change your program to work with getaddrinfo(), you make it future-proof.)

    0 讨论(0)
提交回复
热议问题