client.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // client.cpp
  3. // ~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio.hpp>
  11. #include <boost/bind.hpp>
  12. #include <iostream>
  13. #include <vector>
  14. #include "connection.hpp" // Must come before boost/serialization headers.
  15. #include <boost/serialization/vector.hpp>
  16. #include "stock.hpp"
  17. namespace s11n_example {
  18. /// Downloads stock quote information from a server.
  19. class client
  20. {
  21. public:
  22. /// Constructor starts the asynchronous connect operation.
  23. client(boost::asio::io_context& io_context,
  24. const std::string& host, const std::string& service)
  25. : connection_(io_context.get_executor())
  26. {
  27. // Resolve the host name into an IP address.
  28. boost::asio::ip::tcp::resolver resolver(io_context);
  29. boost::asio::ip::tcp::resolver::query query(host, service);
  30. boost::asio::ip::tcp::resolver::iterator endpoint_iterator =
  31. resolver.resolve(query);
  32. // Start an asynchronous connect operation.
  33. boost::asio::async_connect(connection_.socket(), endpoint_iterator,
  34. boost::bind(&client::handle_connect, this,
  35. boost::asio::placeholders::error));
  36. }
  37. /// Handle completion of a connect operation.
  38. void handle_connect(const boost::system::error_code& e)
  39. {
  40. if (!e)
  41. {
  42. // Successfully established connection. Start operation to read the list
  43. // of stocks. The connection::async_read() function will automatically
  44. // decode the data that is read from the underlying socket.
  45. connection_.async_read(stocks_,
  46. boost::bind(&client::handle_read, this,
  47. boost::asio::placeholders::error));
  48. }
  49. else
  50. {
  51. // An error occurred. Log it and return. Since we are not starting a new
  52. // operation the io_context will run out of work to do and the client will
  53. // exit.
  54. std::cerr << e.message() << std::endl;
  55. }
  56. }
  57. /// Handle completion of a read operation.
  58. void handle_read(const boost::system::error_code& e)
  59. {
  60. if (!e)
  61. {
  62. // Print out the data that was received.
  63. for (std::size_t i = 0; i < stocks_.size(); ++i)
  64. {
  65. std::cout << "Stock number " << i << "\n";
  66. std::cout << " code: " << stocks_[i].code << "\n";
  67. std::cout << " name: " << stocks_[i].name << "\n";
  68. std::cout << " open_price: " << stocks_[i].open_price << "\n";
  69. std::cout << " high_price: " << stocks_[i].high_price << "\n";
  70. std::cout << " low_price: " << stocks_[i].low_price << "\n";
  71. std::cout << " last_price: " << stocks_[i].last_price << "\n";
  72. std::cout << " buy_price: " << stocks_[i].buy_price << "\n";
  73. std::cout << " buy_quantity: " << stocks_[i].buy_quantity << "\n";
  74. std::cout << " sell_price: " << stocks_[i].sell_price << "\n";
  75. std::cout << " sell_quantity: " << stocks_[i].sell_quantity << "\n";
  76. }
  77. }
  78. else
  79. {
  80. // An error occurred.
  81. std::cerr << e.message() << std::endl;
  82. }
  83. // Since we are not starting a new operation the io_context will run out of
  84. // work to do and the client will exit.
  85. }
  86. private:
  87. /// The connection to the server.
  88. connection connection_;
  89. /// The data received from the server.
  90. std::vector<stock> stocks_;
  91. };
  92. } // namespace s11n_example
  93. int main(int argc, char* argv[])
  94. {
  95. try
  96. {
  97. // Check command line arguments.
  98. if (argc != 3)
  99. {
  100. std::cerr << "Usage: client <host> <port>" << std::endl;
  101. return 1;
  102. }
  103. boost::asio::io_context io_context;
  104. s11n_example::client client(io_context, argv[1], argv[2]);
  105. io_context.run();
  106. }
  107. catch (std::exception& e)
  108. {
  109. std::cerr << e.what() << std::endl;
  110. }
  111. return 0;
  112. }