boost::asio::ip::multicast::join_group does not work

后端 未结 2 604
轮回少年
轮回少年 2021-01-04 12:50

I tried the example, but it does not work. Apparently it does not set IPPROTO_IP/IP_MULTICAST_IF option. I can only find boost::asio::ip::multicast::outbound_interface for

相关标签:
2条回答
  • 2021-01-04 13:17

    I think there's an error in boost example code for udp multicast.

    In the example code they bind socket to local interface but for udp multicast you have to bind to the udp multicast group IP and port.

    socket_.bind(listen_endpoint);
    

    should be:

    socket_.bind(
        boost::asio::ip::udp::endpoint( multicast_address, multicast_port ) );
    

    see multicast howto:

    ... for a process to receive multicast datagrams it has to ask the kernel to join the group and bind the port those datagrams were being sent to. The UDP layer uses both the destination address and port to demultiplex the packets and decide which socket(s) deliver them to ...

    ... it is necessary to advise the kernel which multicast groups we are interested in. That is, we have to ask the kernel to "join" those multicast groups ...

    check if you joined the group on correct interface with netstat -g | grep <multicast_group_ip>

    this is I believe incorrect boost example code:

    boost::asio::ip::udp::endpoint listen_endpoint(
        listen_address, multicast_port);
    socket_.open(listen_endpoint.protocol());
    socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    socket_.bind(listen_endpoint);
    
    // Join the multicast group.
    socket_.set_option(
        boost::asio::ip::multicast::join_group(multicast_address));
    
    socket_.async_receive_from(
        boost::asio::buffer(data_, max_length), sender_endpoint_,
        boost::bind(&receiver::handle_receive_from, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    
    0 讨论(0)
  • 2021-01-04 13:17

    Correct Answer:

    boost::asio::ip::udp::endpoint listen_endpoint(udp::v4(), multicast_port); 
    ...
    socket_.set_option(multicast::join_group(
      address::from_string(multicast_address).to_v4(), 
      address::from_string(interface).to_v4()));
    
    0 讨论(0)
提交回复
热议问题