我尝试了example,但它不起作用。显然它没有设置IPPROTO_IP / IP_MULTICAST_IF选项。我只能为IPPROTO_IP / IP_MULTICAST_IF找到boost :: asio :: ip :: multicast :: outbound_interface,我试过但失败了。有没有办法让boost :: asio :: ip :: multicast工作而不需要调用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));
答案 0 :(得分:5)
正确答案:
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()));
答案 1 :(得分:5)
我认为boost example code for udp multicast中存在错误。
在示例代码中,它们将套接字绑定到本地接口,但对于udp多播,您必须绑定到udp多播组IP和端口。
socket_.bind(listen_endpoint);
应该是:
socket_.bind(
boost::asio::ip::udp::endpoint( multicast_address, multicast_port ) );
请参阅multicast howto:
...对于接收组播数据报的进程,它必须询问内核 加入组并绑定端口,这些数据报正在发送 至。 UDP层使用目标地址和端口 解复用数据包并决定将哪些套接字传递给...
...有必要告诉内核我们是哪个组播组 感兴趣的是。我们必须要求内核“加入”那些 组播组......
检查您是否使用netstat -g | grep <multicast_group_ip>
这是我认为错误的提升示例代码:
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));