boost :: asio :: read_until不接受套接字作为参数

问题描述

read可以使用boost::asio::read_until套接字读取,因此我尝试这样做:

using namespace boost::asio;
  ...
    boost::array<char,BUFFER_SIZE> buf;
    read_until(this->socket_opt.value(),buffer(buf),"\n");

但是显然,read_until无法接受这些参数:

error: no matching function for call to ‘read_until(const boost::asio::basic_stream_socket<boost::asio::ip::tcp>&,boost::asio::mutable_buffers_1,const char [2])’
        read_until(this->socket_opt.value(),"\n");

我在做什么错了?

P.S。 socket_opt定义为std::optional<boost::asio::ip::tcp::socket> socket_opt,并在运行时具有一个值。

解决方法

boost::asio::read_until()需要动态缓冲区作为第二个参数。

希望以下代码对您有所帮助。

boost::asio::streambuf buf;
boost::asio::read_until(this->socket_opt.value(),buf,"\n");