简体   繁体   中英

boost asio multicast join

I've made a mistake in my program using boost::asio, in that when receiving multicast data, it binds to the multicast address instead of the local interface ip. There's no error. Somehow on the multi-home linux host, it still received multicast data. But when I run it on another multi-home box, it couldn't. What did this do?

boost::asio::ip::address multicast_address( boost::asio::ip::address::from_string( "239.1.1.100" ) );
unsigned port( 12345 );
boost::asio::io_service io;
boost::asio::ip::udp::socket socket;

boost::asio::ip::udp::endpoint listen_endpoint( multicast_address, multicast_port );
socket.open( listen_endpoint.protocol() );
socket.set_option( boost::asio::ip::udp::socket::reuse_address( true ) );
socket.bind( listen_endpoint );
socket.set_option( boost::asio::ip::multicast::join_group( multicast_address ) );

The listen_endpoint should be bound to the address of the local receiver endpoint, ie which network interface to use. Setting it to any() lets boost use the default receiver, eg:

boost::asio::ip::udp::endpoint listen_endpoint
    ( boost::asio::ip::address_v4::any(), multicast_port );

The multicast_address should just be used to join the multicast group, as per the last line.

There's some useful info about it in here: Are you ready for IPV6?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM