问题描述
我尝试过,但它不工作。显然它不设置IPPROTO_IP / IP_MULTICAST_IF选项。我只能找到boost :: asio :: ip :: multicast :: outbound_interface为IPPROTO_IP / IP_MULTICAST_IF,我试过,但失败。有没有什么办法使boost :: asio :: ip ::多播工作,而不调用c级setsockopt?
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));
推荐答案
我认为的示例代码。
在示例中代码,它们将套接字绑定到本地接口,但对于udp组播,您必须绑定到udp组播组IP和端口。
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);
应为:
socket_.bind(
boost::asio::ip::udp::endpoint( multicast_address, multicast_port ) );
请参阅:
...我们必须要求内核加入那些
多播组...
... 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 ...
检查您是否在 netstat -g | grep< multicast_group_ip>
这是我相信不正确的boost示例代码:
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));
这篇关于boost :: asio :: ip :: multicast :: join_group不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!