boost :: Asio连接错误的界面
boost::asio joins wrong interface
我有一个双重播放设置,每个多播组都需要连接到我的服务器上的特定接口。
coliru
#include <boost/asio.hpp>
#include <iostream>
namespace ip = boost::asio::ip;
using ip::udp;
boost::asio::io_service io;
struct Connection {
int32_t timeout = 5000;
udp::socket sock {io};
ip::address addr;
bool Connect(std::string const& localAddr, std::string const& addrStr, int port, boost::system::error_code& ec) {
// Multicast socket
udp::endpoint local(ip::address::from_string(localAddr), port); // leaving host/port unbound doesn't seem to help
std::cout << "Using local " << local << "n";
addr = ip::address::from_string(addrStr);
udp::endpoint multicastEndpoint(addr, port);
sock.open(multicastEndpoint.protocol());
// The commented flags don't seem to have any effect on the findings
//sock.set_option(ip::multicast::enable_loopback());
sock.bind(local, ec);
sock.set_option(ip::multicast::join_group(addr.to_v4()));
sock.set_option(udp::socket::reuse_address(true));
//setsockopt(sock.native(), SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout));
return ec? false : true;
}
};
struct ConnectionPair {
Connection a, b;
bool Connect(std::string const& addrStrA, int portA, std::string const& addrStrB, int portB, boost::system::error_code& ec) {
// Example adresses; Replace with your local adapter addresses
return a.Connect("172.17.0.1", addrStrA, portA, ec)
&& b.Connect("192.168.195.62", addrStrB, portB, ec);
}
};
int main() {
try {
ConnectionPair pair;
boost::system::error_code ec;
// all hosts multicast groups
if (pair.Connect("224.0.0.251", 5656, "224.0.0.1", 5657, ec)) {
std::cout << "Both connected... ";
boost::asio::deadline_timer dlt(io, boost::posix_time::seconds(5));
dlt.wait();
} else {
std::cout << "Connection error: " << ec.message() << "n";
}
} catch(std::exception const& e) {
std::cout << "Exception: '" << e.what() << "'n";
}
std::cout << "Byen";
}
问题是,在使用此连接时,插座A没有获取数据。使用netsh interface ip show join
,它表明两个多播组都在与localAddrB
相对应的接口中连接在一起,而不是在其位置。
使用mdump
加入多播组时,每个组都连接到适当的位置并接收数据。
我无法弄清楚我的代码在哪里出错。
您可以指定可以广播/加入多播组的接口。
http://www.boost.org/doc/libs/1_47_0/boost/asio/ip/detail/socket_option.hpp
这两行:
this->socket.set_option(ip::multicast::outbound_interface(ip::address_v4::from_string(this->address)));
this->socket.set_option(ip::multicast::join_group(joinAddress, listenInterface));
在上下文中:
服务器代码:
this->socket.open(ip::udp::v4()); this->socket.set_option(ip::udp::socket::reuse_address(true)); this->socket.set_option(ip::multicast::enable_loopback(true)); this->socket.set_option(ip::multicast::hops(HOPS)); this->socket.set_option(ip::multicast::outbound_interface(ip::address_v4::from_string(this->address))); this->socket.bind(ip::udp::endpoint(ip::address_v4::from_string(this->address), 0)); // any port this->ioService.run(); // blocking
客户端代码:
udp::endpoint bindEndpoint(ip::address_v4::any(), UDPMulticastServer::PORT); this->socket.open(ip::udp::v4()); this->socket.set_option(ip::udp::socket::reuse_address(true)); this->socket.bind(bindEndpoint); // Join the multicast group on a specific interface ip::address_v4 joinAddress = ip::address_v4::from_string(UDPMulticastServer::MULTICAST_ADDRESS); ip::address_v4 listenInterface = ip::address_v4::from_string(this->address); this->socket.set_option(ip::multicast::join_group(joinAddress, listenInterface)); this->listenForBroadcast(); this->ioService.run(); // blocking
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- boost :: Asio连接错误的界面
- Swig 界面中的类型映射错误