boost asio : how to keep a client connection alive?

标签 boost boost-asio

我尝试创建一个在服务器上保持连接事件的客户端。

但是,当我收到一次数据时,连接已关闭。我不明白为什么。

我想我应该做一个循环,但我们告诉我这不是一个好主意。

class client
{
public:
  client(boost::asio::io_service& io_service,
      const std::string& host, const std::string& service)
    : connection_(io_service)
  {
    // Resolve the host name into an IP address.
    boost::asio::ip::tcp::resolver resolver(io_service);
    boost::asio::ip::tcp::resolver::query query(host, service);
    boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
      resolver.resolve(query);
    boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;

    // Start an asynchronous connect operation.
    connection_.socket().async_connect(endpoint,
        boost::bind(&client::handle_connect, this,
          boost::asio::placeholders::error, ++endpoint_iterator));
  }

  /// Handle completion of a connect operation.
  void handle_connect(const boost::system::error_code& e,
      boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
  {
    if (!e)
    {
      // Successfully established connection. Start operation to read the list
      // of stocks. The connection::async_read() function will automatically
      // decode the data that is read from the underlying socket.
      connection_.async_read(stocks_,
          boost::bind(&client::handle_read, this,
            boost::asio::placeholders::error));
    }
    else
    {
      std::cerr << e.message() << std::endl;
    }
  }

  /// Handle completion of a read operation.
  void handle_read(const boost::system::error_code& e)
  {
    if (!e)
    {
      // Print out the data that was received.
      for (std::size_t i = 0; i < stocks_.size(); ++i)
      {
        std::cout << "Paquet numero " << i << "\n";
        std::cout << "  age: " << stocks_[i].age << "\n";
        std::cout << "  name: " << stocks_[i].nom << "\n";
      }

                // Maybe Should i put something here ?
    }
    else
    {
      // An error occurred.
        std::cerr << "Error : " << e.message() << std::endl;
        connection_.socket().close();
    }

    // or maybe here ?

  }

private:
  /// The connection to the server.
  connection            connection_;
  std::vector<stock>    stocks_;
};

主要看起来像这样:
int main(int argc, char* argv[])
{
  try
  {
    // Check command line arguments.
    if (argc != 3)
    {
      std::cerr << "Usage: client <host> <port>" << std::endl;
      return 1;
    }

    boost::asio::io_service io_service;

    client client(io_service, argv[1], argv[2]);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

最佳答案

您的 asio io_service 没有工作要做。在 handle_read 中,您可以在其中发表评论“也许我应该在这里放一些东西”,您可以安排下一个异步读取操作:

connection_.async_read(stocks_,
      boost::bind(&client::handle_read, this,
        boost::asio::placeholders::error));
通常,如果您进行异步编程,一旦调用了异步处理程序,您将调用下一个异步操作,就像您在异步连接处理程序中所做的那样。

关于 boost asio : how to keep a client connection alive?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298167/

相关文章:

c++ - 将 boost::geometry 多边形转换为 STL 对象

c++ - Boost ASIO - 什么是异步

async_receive 和 async_send 处理程序中的 C++ Boost.Asio 错误

c++ - spirit x3 规则无法在序列解析器中合成 boost::iterator_range 类型的属性

c++ - 在 solaris 上 boost 库信号量

c++ - 这是使用 boost::asio 进行全双工通信的真实场景吗?

c++ - boost::asio io_service::run_one 导致段错误

c++ - 如何将 `std::chrono::milliseconds` 转换为 `boost::posix_time::milliseconds`

c++ - 将标记转换为 char* const* 时,使用 boost 对字符串进行标记失败

c++ - 用assimp加载模型-不需要 boost 吗?