简体   繁体   中英

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.

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. So what would happen? Should I receive a EOF?

Updata: I think @user786653 is right. 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 :

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). Eg something like nc -l 8000 or python -m SimpleHTTPServer . Then run the program and wait. Nothing happens. Now stop the server, on my (Windows) machine this results in:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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