boost :: asio :: async_read_until问题

时间:2010-06-17 01:22:00

标签: c++ boost-asio

我正在尝试从boost asio修改echo服务器示例,当我尝试使用boost :: asio :: async_read_until时,我遇到了问题。这是代码:

    #include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

class session
{
public:
  session(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    std::cout<<"starting"<<std::endl;  
  boost::asio::async_read_until(socket_, boost::asio::buffer(data_, max_length), ' ',
        boost::bind(&session::handle_read, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
  }

  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
    std::cout<<"handling read"<<std::endl;
    if (!error)
    {
      boost::asio::async_write(socket_,
          boost::asio::buffer(data_, bytes_transferred),
          boost::bind(&session::handle_write, this,
            boost::asio::placeholders::error));
    }
    else
    {
      delete this;
    }
  }

  void handle_write(const boost::system::error_code& error)
  {
    if (!error)
    {
    /*
      socket_.async_read_some(boost::asio::buffer(data_, max_length),
          boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
   */ }
    else
    {
      delete this;
    }
  }

private:
  tcp::socket socket_;
  enum { max_length = 1024 };
  char data_[max_length];
};

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    session* new_session = new session(io_service_);
    acceptor_.async_accept(new_session->socket(),
        boost::bind(&server::handle_accept, this, new_session,
          boost::asio::placeholders::error));
  }

  void handle_accept(session* new_session,
      const boost::system::error_code& error)
  {
    if (!error)
    {
      new_session->start();
      new_session = new session(io_service_);
      acceptor_.async_accept(new_session->socket(),
          boost::bind(&server::handle_accept, this, new_session,
            boost::asio::placeholders::error));
    }
    else
    {
      delete new_session;
    }
  }

private:
  boost::asio::io_service& io_service_;
  tcp::acceptor acceptor_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 2)
    {
      std::cerr << "Usage: async_tcp_echo_server <port>\n";
      return 1;
    }

    boost::asio::io_service io_service;

    using namespace std; // For atoi.
    server s(io_service, atoi(argv[1]));

    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

问题是当我尝试编译时,我得到了这个奇怪的错误:

server.cpp:在成员函数'void session :: start()'中: server.cpp:27:错误:没有匹配函数来调用'async_read_until(boost :: asio :: basic_stream_socket&gt;&amp;,boost :: asio :: mutable_buffers_1,char,boost :: _ bi :: bind_t,boost :: _bi :: list3,boost :: arg&lt; 1&gt;()(),boost :: arg&lt; 2&gt;()()&gt;&gt;)'

有人可以解释一下发生了什么吗?从我可以告诉async_read_until的参数是正确的。

谢谢!

3 个答案:

答案 0 :(得分:7)

async_read_until的第二个参数应该是streambuf object into which the data will be read。简单来说,您需要按引用传递boost::asio::streambuf,而不是按值boost::asio::buffer传递。

答案 1 :(得分:0)

const(in)正确性也可能导致此类错误消息。

答案 2 :(得分:0)

无需使用streambufs。有接受动态缓冲区的重载。动态是关键,因为read_until暗示需要一个缓冲区,该缓冲区可以根据调用方的判断来增加缓冲区的大小。因此,您所需要做的就是用boost::asio::buffer替换对boost::asio::dynamic_buffer的调用,然后在std::stringstd::vector<char>上调用它。

...

  void start()
  {
    std::cout<<"starting"<<std::endl;  
    boost::asio::async_read_until(
        socket_, boost::asio::dynamic_buffer(data_), ' ', 
        boost::bind(&session::handle_read, this,
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
  }

...

 private:
  tcp::socket socket_;
  std::string data_;
  // or
  //std::vector<char> data_;
};

我也将摆脱boost::bind,因为这是一种过大的杀伤力,并且使用了lambda(请尽可能多地使用它们)

[this](const boost::system::error_code& error, size_t bytes_transferred) {
  handle_read(error, bytes_transferred);
}