0

基本上我有一个客户端,我向服务器发送一个 10 字节的字符串,hellohello并且在服务器内我希望回复会0123456789返回给客户端,但我会hellohello再次收到?我将 tcp_server.cpp 中的第 58 行更改为char data_char data_out因为我认为这是发送数据包数据的地方?我很确定会被调用,但由于某种原因,事情并没有像我想象的那样工作。

这是服务器输出,

handle read: bytes_transferred10
10
handle write:
0123456789
handle read: bytes_transferred0

我也想知道为什么又handle read: bytes_transferred0被叫了?

这是客户端输出,

Enter message: hellohello
Reply is: hellohello

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

这是 tcp_server.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;
}

tcp_client.cpp

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

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

enum { max_length = 1024 };

int main(int argc, char* argv[])
{
  try
  {


    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query(tcp::v4(), "127.0.0.1", "4000");
    tcp::resolver::iterator iterator = resolver.resolve(query);

    tcp::socket s(io_service);
    s.connect(*iterator);

    using namespace std; // For strlen.
    std::cout << "Enter message: ";
    char request[max_length];
    std::cin.getline(request, max_length);
    size_t request_length = strlen(request);
    boost::asio::write(s, boost::asio::buffer(request, request_length));

    char reply[max_length];
    size_t reply_length = boost::asio::read(s,boost::asio::buffer(reply, request_length));
    std::cout << "Reply is: ";
    std::cout.write(reply, reply_length);
    std::cout << "\n";
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}
4

1 回答 1

1

你的评论

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

接近但不完全正确,因为这里没有递归。该文档很好地解释了这一点:

无论异步操作是否立即完成,都不会在此函数中调用处理程序。处理程序的调用将以等同于 using 的方式执行boost::asio::io_service::post()

增加重点是我的。与递归相比,最好将这些概念视为链接,因为一个操作(例如)async_write()是在另一个操作的处理程序中启动的,例如async_read(). 确切的细节取决于使用的协议。

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

于 2013-04-13T15:31:20.070 回答