使用 use_future 的 Boost Asio async_send_to 多播不起作用
Boost Asio async_send_to multicast with use_future does not work
ITNOA
我有一个程序想用 async_send_to 向一个多播组发送一条消息。但我想这个动作是阻塞的而不是异步的,所以我使用 use_future 来实现这个目标,如下所示
gw::BoostMessageSender::BoostMessageSender(const std::string& group_address, const std::string& interface_name, uint16_t port_number, ba::io_context& io_context)
: AbstractMessageSender(group_address, interface_name, port_number)
, io_context(io_context)
, endpoint(ba::ip::address::from_string(group_address), port_number)
, socket(io_context, endpoint.protocol())
{
if (!socket.is_open())
socket.open(endpoint.protocol());
socket.set_option(ba::ip::udp::socket::reuse_address(true));
}
bool gw::BoostMessageSender::send(const std::string& message) noexcept
{
std::future<std::size_t> result = socket.async_send_to(ba::buffer(message), endpoint, ba::use_future);
return result.get() == message.size();
}
我使用这个 class 如下所示
int main()
{
const string group_address = "235.127.1.1";
constexpr uint16_t PORT_NUMBER = 8765;
boost::asio::io_context io_context;
BoostMessageSender message_sender(group_address, "eth1", PORT_NUMBER, io_context);
std::thread t1([&io_context]()
{
io_context.run();
});
this_thread::sleep_for(1s);
message_sender.send("Any bodey there?");
cout << "send message: "; // --- this line is not run never :(((
io_context.stop();
t1.join();
return 0;
}
我的问题是为什么 send message
永远不会在我的控制台中写入?
陌生人,我用 tcpdump -i eth0 -nnSX port 8765
检查我的网络,在调用 async_send_to
之后我可以看到发送到网络的多播消息,但是 result.get()
从来没有 return 结果并坚持下去。
我哪里错了?
我在以下平台上测试了这些代码
Visual Studio 2019 16.7.4
Windows 10 1909 latest update
Boost 1.73.0
Ubuntu 20.04.1 LTS
GCC 9.3.0
Boost 1.71.0
io_context::run
returns 当没有待执行的作业时。
您可以创建 work guard 以确保 运行 即使没有启动异步任务也不会完成。使用make_work_guard
函数:
boost::asio::io_context io_context;
auto workGuard = boost::asio::make_work_guard(io_context);
ITNOA
我有一个程序想用 async_send_to 向一个多播组发送一条消息。但我想这个动作是阻塞的而不是异步的,所以我使用 use_future 来实现这个目标,如下所示
gw::BoostMessageSender::BoostMessageSender(const std::string& group_address, const std::string& interface_name, uint16_t port_number, ba::io_context& io_context)
: AbstractMessageSender(group_address, interface_name, port_number)
, io_context(io_context)
, endpoint(ba::ip::address::from_string(group_address), port_number)
, socket(io_context, endpoint.protocol())
{
if (!socket.is_open())
socket.open(endpoint.protocol());
socket.set_option(ba::ip::udp::socket::reuse_address(true));
}
bool gw::BoostMessageSender::send(const std::string& message) noexcept
{
std::future<std::size_t> result = socket.async_send_to(ba::buffer(message), endpoint, ba::use_future);
return result.get() == message.size();
}
我使用这个 class 如下所示
int main()
{
const string group_address = "235.127.1.1";
constexpr uint16_t PORT_NUMBER = 8765;
boost::asio::io_context io_context;
BoostMessageSender message_sender(group_address, "eth1", PORT_NUMBER, io_context);
std::thread t1([&io_context]()
{
io_context.run();
});
this_thread::sleep_for(1s);
message_sender.send("Any bodey there?");
cout << "send message: "; // --- this line is not run never :(((
io_context.stop();
t1.join();
return 0;
}
我的问题是为什么 send message
永远不会在我的控制台中写入?
陌生人,我用 tcpdump -i eth0 -nnSX port 8765
检查我的网络,在调用 async_send_to
之后我可以看到发送到网络的多播消息,但是 result.get()
从来没有 return 结果并坚持下去。
我哪里错了?
我在以下平台上测试了这些代码
Visual Studio 2019 16.7.4
Windows 10 1909 latest update
Boost 1.73.0
Ubuntu 20.04.1 LTS
GCC 9.3.0
Boost 1.71.0
io_context::run
returns 当没有待执行的作业时。
您可以创建 work guard 以确保 运行 即使没有启动异步任务也不会完成。使用make_work_guard
函数:
boost::asio::io_context io_context;
auto workGuard = boost::asio::make_work_guard(io_context);