Home > Enterprise >  error: ‘async_read_until’ is not a member of ‘boost::asio’
error: ‘async_read_until’ is not a member of ‘boost::asio’

Time:03-08

Code: https://coliru.stacked-crooked.com/a/d39bdc13f47785e5

Reference:

https://www.boost.org/doc/libs/1_72_0/libs/beast/doc/html/beast/using_io/timeouts.html https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/reference/async_read_until.html

/** This function echoes back received lines from a peer, with a timeout.

    The algorithm terminates upon any error (including timeout).
*/
template <class Protocol, class Executor>
void do_async_echo (basic_stream<Protocol, Executor>& stream)
{
    // This object will hold our state when reading the line.

    struct echo_line
    {
        basic_stream<Protocol, Executor>& stream;

        // The shared pointer is used to extend the lifetime of the
        // string until the last asynchronous operation completes.
        std::shared_ptr<std::string> s;

        // This starts a new operation to read and echo a line
        void operator()()
        {
            // If a line is not sent and received within 30 seconds, then
            // the connection will be closed and this algorithm will terminate.

            stream.expires_after(std::chrono::seconds(30));

            // Read a line from the stream into our dynamic buffer, with a timeout
            net::async_read_until(stream, net::dynamic_buffer(*s), '\n', std::move(*this));
        }

        // This function is called when the read completes
        void operator()(error_code ec, std::size_t bytes_transferred)
        {
            if(ec)
                return;

            net::async_write(stream, buffers_prefix(bytes_transferred, net::buffer(*s)),
                [this](error_code ec, std::size_t bytes_transferred)
                {
                    s->erase(s->begin(), s->begin()   bytes_transferred);

                    if(! ec)
                    {
                        // Run this algorithm again
                        echo_line{stream, std::move(s)}();
                    }
                    else
                    {
                        std::cerr << "Error: " << ec.message() << "\n";
                    }
                });
        }
    };

    // Create the operation and run it
    echo_line{stream, std::make_shared<std::string>()}();
}


$ g   -std=gnu  17 -I ./boost/boost_1_72_0. timeout_example.cpp -o ./build/timeout_example
timeout_example.cpp: In member function ‘void do_async_echo(boost::beast::basic_stream<Protocol, Executor>&)::echo_line::operator()()’:
timeout_example.cpp:43:18: error: ‘async_read_until’ is not a member of ‘boost::asio’; did you mean ‘async_result’?
   43 |             net::async_read_until(stream, net::dynamic_buffer(*s), '\n', std::move(*this));
      |                  ^~~~~~~~~~~~~~~~
      |                  async_result

Question> Why does the compiler(g (GCC) 10.2.1) cannot find the boost::asio::async_read_until?

Thank you

CodePudding user response:

You are missing the correct header file. Include

#include <boost/asio/read_until.hpp>

or

#include <boost/asio.hpp>
  • Related