理解boost::asio-async_read在无需读取内容时的行为
understand boost::asio async_read behavior when there is nothing to read
我正试图了解async_read
在没有可读取内容时会发生什么。
例如,客户端创建与服务器的连接,然后启动async_read()
,但该服务器不希望向该客户端发送任何内容。那么会发生什么呢?我应该接受EOF吗?
更新数据:我认为@user786653是对的。我做了一个简单的例子(见下文(。
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>
class test{
public:
test(boost::asio::io_service& io_service):_socket(io_service){
}
void handle_connect(){
std::cout<<"entering test::handle_connect"<<std::endl;
char reply[128];
boost::asio::async_read(_socket, boost::asio::buffer(reply, sizeof(reply)),
[](boost::system::error_code ec, std::size_t /*length*/){
std::cout<<"Read result:"<< ec<<" - "<<ec.message()<<std::endl;
});
}
boost::asio::ip::tcp::socket & socket(){
return _socket;
}
private:
boost::asio::ip::tcp::socket _socket;
};
int main() {
try {
boost::asio::io_service io_service;
boost::asio::ip::tcp::socket s(io_service);
boost::asio::ip::tcp::resolver resolver(io_service);
boost::asio::ip::tcp::resolver::query query("127.0.0.1", "8000");
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
test t(io_service);
t.socket().async_connect(endpoint,boost::bind(&test::handle_connect, &t));
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "n";
}
}
引用最新(1.68.0(文档:
此函数用于从流中异步读取特定字节数的数据。函数调用总是立即返回。异步操作将继续,直到以下条件之一成立:
- 提供的缓冲区已满。也就是说,传输的字节等于缓冲区大小之和
- 出现错误
因此,在服务器关闭连接之前不会发生任何事情(导致错误(。
你可以自己测试一下:
#include <iostream>
#include <boost/asio.hpp>
int main() {
try {
boost::asio::io_context io_context;
boost::asio::ip::tcp::socket s(io_context);
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::connect(s, resolver.resolve("localhost", "8000"));
char reply[128];
async_read(s, boost::asio::buffer(reply, sizeof(reply)), [](boost::system::error_code ec, std::size_t /*length*/) {
std::cout << "Read result: " << ec << " - " << ec.message() << "n";
});
io_context.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "n";
}
}
在localhost端口8000上启动一个没有响应的服务器(或更改代码(。例如nc -l 8000
或python -m SimpleHTTPServer
之类的东西。然后运行程序并等待。什么也没发生。现在停止服务器,在我的(Windows(机器上这会导致:
Read result: system:10054 - An existing connection was forcibly closed by the remote host
相关文章:
- 理解boost::asio-async_read在无需读取内容时的行为
- Async_read_until限制读取的字节大小(Boost::asio)
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 增强 ASIO 和串行端口异步读取
- boost::asio 不会触发读取处理程序,而 Wireshark 会看到传入的数据
- 异步提升从stdin读取的asio
- 如何使用 boost::asio 的 async_read_some() 读取所有可用数据,而无需等待新数据到达?
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 提升 ASIO transfer_exactly读取 0 字节
- C++从 std::async 函数读取命名空间中的全局变量标志
- boost::asio::async_read_until 不会读取所有消息
- 如何在async_read_until后使用 asio 缓冲区进行连续读取
- 使用 boost::asio 读取 JSON 流,获取完整字符串
- 提升ASIO写入/读取向量
- 是否可以在使用 Boost::asio 之前从套接字读取后执行async_handshake
- 提升::不满足 ASIO 读取处理程序类型要求
- Boost ASIO async_read 没有从服务器读取数据
- boost::asio async condition
- 读取文件async-iso文件c++
- 读取:ASIO中的文件结束.插座为什么要关上