Boost :: asio :: ip :: multicast :: join_group not working

I tried example , but it does not work. It does not seem to set the IPPROTO_IP / IP_MULTICAST_IF parameter. I can only find boost :: asio :: ip :: multicast :: outbound_interface for IPPROTO_IP / IP_MULTICAST_IF, I tried, but could not. Is there any way to make boost :: asio :: ip :: multicast work without calling c-level setsockopt?

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)); 
+8
c ++ boost-asio
source share
2 answers

I think there is an error in the sample code example for udp multicast .

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

 socket_.bind(listen_endpoint); 

it should be:

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

see multicast :

... for the process of receiving multicast datagrams, he must ask the kernel to join the group and bind the port where the datagrams are sent to. The UDP level uses both the destination address and the port to demultiplex packets and decide which socket to deliver them to ...

... you need to tell the kernel which multicast groups we are interested in. That is, we must ask the kernel to "join" those multicast groups ...

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

I believe the incorrect boost example code is:

 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)); 
+5
source share

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())); 
+4
source share

All Articles