简体   繁体   English

当没有什么要阅读时,了解boost :: asio async_read行为

[英]understand boost::asio async_read behavior when there is nothing to read

I am trying to understand what would happen with async_read when there is nothing to read. 我试图了解什么也没读的情况下,使用async_read会发生什么。

For example, a client creates a connection to a server, then start async_read() , but that server does not expect to send anything to this client. 例如,客户端创建与服务器的连接,然后启动async_read() ,但该服务器不希望向该客户端发送任何内容。 So what would happen? 那会发生什么呢? Should I receive a EOF? 我应该收到EOF吗?

Updata: I think @user786653 is right. Updata:我认为@ user786653是正确的。 I made a simple example (see following). 我做了一个简单的例子(见下文)。

#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";
}
}

Quoting from the latest (1.68.0) documentation : 引用最新(1.68.0) 文档

This function is used to asynchronously read a certain number of bytes of data from a stream. 此函数用于从流中异步读取一定数量的数据字节。 The function call always returns immediately. 函数调用总是立即返回。 The asynchronous operation will continue until one of the following conditions is true: 异步操作将继续,直到满足以下条件之一:

  • The supplied buffers are full. 提供的缓冲区已满。 That is, the bytes transferred is equal to the sum of the buffer sizes. 即,传输的字节等于缓冲区大小的总和。
  • An error occurred. 发生错误。

So nothing will happen until the server closes the connection (resulting in an error). 因此,在服务器关闭连接之前不会发生任何事情(导致错误)。

You can test this out for yourself: 您可以自己测试一下:

#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";
    }
}

Start a server that doesn't respond on localhost port 8000 (or change the code). 启动在本地主机端口8000上没有响应的服务器(或更改代码)。 Eg something like nc -l 8000 or python -m SimpleHTTPServer . 例如nc -l 8000python -m SimpleHTTPServer Then run the program and wait. 然后运行程序并等待。 Nothing happens. 什么都没发生。 Now stop the server, on my (Windows) machine this results in: 现在停止服务器,在我的(Windows)机器上,结果是:

Read result: system:10054 - An existing connection was forcibly closed by the remote host

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM