boost::asio::ip::multicast::join_group does not work
boost::asio::ip::multicast::join_group does not work
我尝试了这个例子,但它不起作用。显然,它没有设置IPPROTO_IP/IP_MULTICAST_IF选项。我只能找到boost::asio::ip::multicast::outbound_interface的IPPROTO_IP/IP_MULTICAST_IF,我尝试了,但失败了。是否有任何方法使boost::asio::ip::多播工作而不调用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));
正确答案:
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()));
我认为在udp组播的boost示例代码中存在错误。
在示例代码中,它们将套接字绑定到本地接口,但对于udp多播,您必须绑定到udp多播组的IP和端口。
socket_.bind(listen_endpoint);
应:socket_.bind(
boost::asio::ip::udp::endpoint( multicast_address, multicast_port ) );
参见multicast howto:
…对于接收多播数据报的进程,它必须请求内核要加入组并绑定正在发送的数据报的端口出现。UDP层同时使用目的地址和端口to将数据包拆分并决定哪个套接字将它们发送到…
…有必要通知内核我们是哪个多播组感兴趣的。也就是说,我们必须要求内核"连接"它们组播组…
检查您是否在正确的接口与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));
相关文章:
- 将系数存储在头文件的数组中("does not name a type"错误)
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 嵌套类"does not name a type"
- "what does ":*** [可执行文件] 错误 1 " mean ?"
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- Eigen loadMarket does not compile
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 创建C++ Redis 模块 - "does not export RedisModule_OnLoad() symbol"
- 无法分隔标头类。我得到"does not name a type"
- 模板元编程:"does not have integral or enumeration type"
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- #define 的"Declaration does not declare anything"错误
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- C++ 迭代器错误"does not refer to a value"