c ++:BOOST_ASIO服务器回复不符合预期?

时间:2013-04-13 01:47:18

标签: c++ client-server boost-asio output reply

基本上我有一个客户端,我将一个10字节hellohello的字符串发送到服务器,在服务器内我期望回复为0123456789回客户端,但我得到{{1再次?我在tcp_server.cpp的第58行将hellohello更改为char data_,因为我认为这是发送数据包数据的地方?我很确定会被调用,但出于某种原因,事情并没有像我想的那样发挥作用。

这是服务器输出

char data_out

我也想知道为什么handle read: bytes_transferred10 10 handle write: 0123456789 handle read: bytes_transferred0 再次被召唤?

这是客户端输出

handle read: bytes_transferred0

这是tcp_server.cpp

Enter message: hellohello
Reply is: hellohello

Process returned 0 (0x0)   execution time : 6.484 s
Press any key to continue.

tcp_client.cpp

#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()
  {
          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));
  }
void read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred);


private:
  void handle_read(const boost::system::error_code& error,
      size_t bytes_transferred)
  {
      std::cout<<"handle read: bytes_transferred"<<bytes_transferred<<std::endl;
    if (!error)
    {
          read_handler(error, bytes_transferred);
          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)
  {
      std::cout<<"handle write: "<<std::endl;
      data_out = {'0','1','2','3','4','5','6','7','8','9'};
    if (!error)
    {
            std::cout<<data_out<<std::endl;

            socket_.async_read_some(boost::asio::buffer(data_out, max_length),
            boost::bind(&session::handle_read, this,
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred));
    }
    else
    {
      delete this;
    }
  }

  tcp::socket socket_;
  enum { max_length = 1024 };
  char data_[max_length];
  char data_out[max_length];
};
void session::read_handler(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
      std::cout<<bytes_transferred<<std::endl;
}

class server
{
public:
  server(boost::asio::io_service& io_service, short port)
    : io_service_(io_service),
      acceptor_(io_service, tcp::endpoint(tcp::v4(), port))
  {
    start_accept();
  }


private:
  void start_accept()
  {
    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();
    }
    else
    {
      delete new_session;
    }

    start_accept();
  }

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

int main()
{
  try
  {
    boost::asio::io_service io_service;
    server s(io_service, 4000);

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

  return 0;
}

1 个答案:

答案 0 :(得分:1)

您的评论

  

我认为handle_read()递归调用handle_write()

接近但不太正确,因为这里没有递归。 documentation很好地解释了这一点:

  

无论异步操作是否立即完成   或者,不会从此函数中调用处理程序。   处理程序的调用将以相当于的方式执行   使用boost::asio::io_service::post()

增加了重点是我的。而不是递归,最好将这些概念视为链接,因为async_write()之类的一个操作是在另一个操作中启动的,例如async_read()。具体细节取决于使用的协议。

如果您希望服务器将字符串0123456789发送到客户端,请在调用async_write()之前填充缓冲区。