boost :: asio :: async_write没有完成它的任务

时间:2010-06-17 13:43:08

标签: c++ boost boost-asio

我试图通过操作echo示例来弄清楚异步读取和写入如何在boost asio中工作。目前,我有一个服务器,当发送一个句子时,应该只回应第一个单词。但是,即使正在调用写入处理程序,boost :: asio :: async_write也似乎永远不会完成。有人可以解释一下发生了什么吗?这是代码:

#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_, buffer, ' ',
        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::ostringstream ss;
// ss<<&buffer;

 char* c = new char[bytes_transferred]; 

 //std::string s;
 buffer.sgetn(c,bytes_transferred);
 std::cout<<"data: "<< c<<" bytes: "<<bytes_transferred<<std::endl;

 if (!error)
    {
      boost::asio::async_write(socket_,
          boost::asio::buffer(c,bytes_transferred),
          boost::bind(&session::handle_write, this,
            boost::asio::placeholders::error));
    }
    else
    {
      delete this;
    }
  }

  void handle_write(const boost::system::error_code& error)
  {
 std::cout<<"handling write"<<std::endl;
    if (!error)
    {
    }
    else
    {
      delete this;
    }
  }

private:
  tcp::socket socket_;
  boost::asio::streambuf buffer;
};

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

谢谢!

3 个答案:

答案 0 :(得分:2)

你没有正确使用boost :: asio :: streambuf,请仔细阅读HTTP client example。对于您的代码,您应该执行以下操作:

buffer.commit( bytes_transferred );
std::istream is( &buffer );
std::string data;
is >> data;
std::cout << "data: " << data << " bytes: " << bytes_transferred << std::endl;

然后使用单独的boost :: asio :: streambuf作为您的回复。正如之前的回答所说,你正在泄露记忆。我建议使用boost :: shared_ptr和shared_from_this。

答案 1 :(得分:0)

可以看到你只是不关闭套接字甚至不删除会话。 成功编写并完成写入处理程序后,会使会话对象中断内存。此外,char* c = new char[bytes_transferred];会导致内存泄漏,因为它永远不会再次使用,boost::asio::buffer不会为您释放内存。

只需更改写处理程序:

void handle_write(const boost::system::error_code& error)
{
  std::cout<<"handling write and exiting."<<std::endl;
  delete this;
}

答案 2 :(得分:0)

  1. 为了避免涉及Session的可能的memmory泄漏问题,您应该考虑使用boost::enable_shared_from_this,如下所示:

    class session : public boost::enable_shared_from_this<session>
    

    不使用“this”而是使用shared_from_this()

    请勿使用delete this

  2. 您按new分配表格,但我不知道内存是免费的。

    char* c = new char[bytes_transferred]; 
    

    delete c没有被称为

  3. 也许更好的解决方案是使用额外的缓冲区m_bufferForRead而不是分配表c