- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在学习 Boost.Asio。我创建了一个简单的程序来将主机名解析为 IP 地址。使用同步解析操作时它工作正常。但是,当我尝试异步方式时,出现了一些奇怪的行为。
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
using boost::asio::ip::tcp;
void resolver_handler(
const boost::system::error_code& err,
tcp::resolver::iterator it
) {
if (err) {
std::cerr << "Resolver error: " << err.message() << std::endl;
return;
}
tcp::resolver::iterator end;
while (it != end) {
std::cout << "Host name: " << it->host_name() << std::endl;
std::cout << "Endpoint: " << it->endpoint() << std::endl;
std::cout << "Service name: " << it->service_name() << std::endl;
++it;
}
}
void resolve_host(boost::asio::io_service& io_service) {
tcp::resolver::query query("www.google.com", "http");
tcp::resolver resolver(io_service);
resolver.async_resolve(
query,
boost::bind(
resolver_handler,
boost::asio::placeholders::error,
boost::asio::placeholders::iterator
)
);
std::cout << "Bind" << std::endl; // <<<----This line
}
int main(int argc, char **argv) {
try {
boost::asio::io_service io_service;
resolve_host(io_service);
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
当resolve_host
函数的最后一行被注释掉时,报告
Resolver error: The I/O operation has been aborted because of either a thread exit or an application request
当该行出现时,它会给出正确的输出
Bind
Host name: www.google.com
Endpoint: 216.58.219.4:80
Service name: http
我所做的是打印出一些东西。我试图在 async_resolve
调用之后添加一些更简单的逻辑(例如 int a = 1;
),但它不起作用。在我看来,这是一个时间问题。也许某些东西退出得太快了。
我搜索此错误消息,但发现大多数帖子都是关于 C# 的。我相信这个错误消息不是来自 Boost,而是来自 Windows 系统。
谁能给我解释一下为什么会这样?非常感谢。
最佳答案
正如@David Schwartz 所说,您必须让解析器保持事件状态才能完成异步操作。
这是我能想到的最简单的方法:
void resolve_host(boost::asio::io_service& io_service) {
tcp::resolver::query query("www.google.com", "http");
auto resolver = std::make_shared<tcp::resolver>(io_service);
resolver->async_resolve(
query,
[resolver](auto ec, auto it) { resolver_handler(ec, it); }
);
}
请注意,我使用了 lambda 来捕获解析器(它是动态分配的,而不是在堆栈上分配的)。
如果您没有该选项,最简单的方法是将一些不透明的上下文绑定(bind)到完成处理程序 bind
:
using opaque_context = std::shared_ptr<void const>;
void resolver_handler(
const boost::system::error_code& err,
tcp::resolver::iterator it,
opaque_context
) {
// ...
然后使用
resolver->async_resolve(
query,
boost::bind(resolver_handler, ph::error, ph::iterator, resolver)
);
参见在 Coliru 上编译:c++03 only , C++11 opaque_context
, c++11 lambda
请注意,我可能会传递 endpoint
而不是 iterator
以进一步解耦代码(参见例如 ASIO getting a tcp endpoint directly from an asynchronous resolve )。但您不需要这样做,因为迭代器
确实 使关联状态保持事件状态:What's the lifetime of boost::asio::ip::tcp::resolver::iterator from async_resolve?
关于c++ - boost .Asio : The I/O operation has been aborted because of either a thread exit or an application request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254109/
使用 asio 库,我想为 asio::serial_port 读/写调用使用超时。 是否可以使用相同的 asio::serial_port asio::io_context 和用于 asio 的相同
对于我正在从事的副业项目应该使用哪种类型的解析器,我有点困惑。我在 asio 文档中找不到答案。 我知道 DNS 可以与 UDP 或 TCP 一起使用,并且通常通过 TCP 发送较大的响应。 asio
在仅从一个线程调用 io_service::run() 的情况下,从不同线程调用 async_write 和 async_read 是否安全?谢谢! 最佳答案 Is it safe to call a
我想知道Boost ASIO 有多受欢迎。它是否被用于任何流行的网络密集型软件中? 最佳答案 用于管理 IBM Blue Gene/Q 的系统软件 super 计算机广泛使用Boost.Asio。
我想使用一个函数来读取套接字端口,并在收到 IP 数据包时交还控制权。 boost::asio::ip::udp::socket 有一个函数接收(或 async_receive),它返回读取了多少字节
我试图调整 Boost 文档中的 SSL 服务器示例 here但我想制作一个应用程序,您可以在其中使用普通 boost::asio::ip::tcp::socket或 SSL 套接字,但我还没有找到将
在查看 boost asio co_spawn 文档 ( https://www.boost.org/doc/libs/1_78_0/doc/html/boost_asio/reference/co_
我正在尝试使用 Boost.ASIO 库,但我找不到如何列出 boost 的可用端口(带有串行端口服务)或套接字(带有网络服务)。 你知道这是否可能吗? 谢谢你。 最佳答案 Boost.Asio 不提
我想使用boost::asio从多个stdout中同时读取stderr和boost::process。但是,我在使用boost::asio时遇到了编译问题,可以重建以下无法编译的最小示例: #incl
提前为一个愚蠢的问题道歉 - 我对这一切都很陌生。 所以我从 here 下载了 asio ,并尝试#include asio.hpp,但出现以下错误; fatal error: boost/confi
我是使用 boost 的项目的一部分作为一个 C++ 库。现在我们要使用 SMTP/POP3/SSL/HTTP/HTTPS。我在 Poco::Net 中检测到几个拟合类和函数 Poco::Net::P
有谁知道有任何实现 Web Sockets 的尝试吗?使用 Boost asio 的 API? 最佳答案 我意识到这是一个旧线程,但想更新以帮助那些寻找答案的人:WebSocket++完全符合要求。
和 asio::thread_pool 有什么区别和一个 asio::io_context谁的run()函数是从多个线程调用的?我可以更换我的 boost::thread_group调用 io_con
我想连接到由目标 IP 地址和端口号指定的服务器套接字。 boost::asio::connect 似乎不允许使用它。我有 ip 目的地作为无符号 int 值。 更新:我能够做到 ba::ip::tc
我在 pc 上有 3 个网络接口(interface),并且想确保当我进行 udp 套接字发送时,它通过特定的网络接口(interface)发送(我有发送数据时使用的 ip 地址)。 这是代码。 ud
我正在使用 ASIO 开发网络应用程序并提到了Chat-Server/Client 我问过类似的问题Here 为了更好地解释,我在这里添加了更多代码: 我的 Cserver Class class C
我已经阅读了 boost asio 引用资料,浏览了教程并查看了一些示例。尽管如此,我还是看不出应该如何拆除套接字: 我应该调用 close() 还是由套接字的析构函数完成? 什么时候应该调用 shu
我认为标题已经说明了大部分内容,但我也有兴趣了解在没有现有解决方案的情况下如何将 DTLS 支持引入 asio 最佳答案 ASIO 本身不支持DTLS 但有一个GitHub 库asio_dtls已向
我正在将 async_read 与 streambuf 一起使用。但是,我想将读取的数据量限制为 4,这样我就可以在进入正文之前正确处理 header 。 我如何使用 async_read 做到这一点
从this example开始,我想用 async_read_until() 替换 async_read()。 所以我查了一下this example ,并查看了如何调用 async_read_unt
我是一名优秀的程序员,十分优秀!