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

匿名 (未验证) 提交于 2019-12-03 02:20:02

问题:

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 IPPROTO_IP/IP_MULTICAST_IF, I tried but failed. 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)); 

回答1:

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)); 


回答2:

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())); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!