我试过example,但它不工作。显然,它没有设置IPPROTO_IP/IP_MULTICAST_IF选项。我只能找到IPPROTO_IP/IP_MULTICAST_IF的boost::asio::ip::multicast::outbound_interface,我尝试过,但失败了。有没有办法让boost::asio::ip::multicast在不调用c级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));发布于 2011-12-31 06:53:59
正确答案:
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()));发布于 2013-01-11 14:42:00
我认为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示例代码:
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));https://stackoverflow.com/questions/8675623
复制相似问题