//// client.cpp// ~~~~~~~~~~//// Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)//// Distributed under the Boost Software License, Version 1.0. (See accompanying// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)//#include <boost/asio.hpp>#include <functional>#include <iostream>#include <vector>#include "connection.hpp" // Must come before boost/serialization headers.#include <boost/serialization/vector.hpp>#include "stock.hpp"namespace s11n_example {/// Downloads stock quote information from a server.class client{public:/// Constructor starts the asynchronous connect operation.client(boost::asio::io_context& io_context,const std::string& host, const std::string& service): connection_(io_context.get_executor()){// Resolve the host name into a sequence of endpoints.boost::asio::ip::tcp::resolver resolver(io_context);auto endpoints = resolver.resolve(host, service);// Start an asynchronous connect operation.boost::asio::async_connect(connection_.socket(), endpoints,std::bind(&client::handle_connect, this,boost::asio::placeholders::error));}/// Handle completion of a connect operation.void handle_connect(const boost::system::error_code& e){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_,std::bind(&client::handle_read, this,boost::asio::placeholders::error));}else{// An error occurred. Log it and return. Since we are not starting a new// operation the io_context will run out of work to do and the client will// exit.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 << "Stock number " << i << "\n";std::cout << " code: " << stocks_[i].code << "\n";std::cout << " name: " << stocks_[i].name << "\n";std::cout << " open_price: " << stocks_[i].open_price << "\n";std::cout << " high_price: " << stocks_[i].high_price << "\n";std::cout << " low_price: " << stocks_[i].low_price << "\n";std::cout << " last_price: " << stocks_[i].last_price << "\n";std::cout << " buy_price: " << stocks_[i].buy_price << "\n";std::cout << " buy_quantity: " << stocks_[i].buy_quantity << "\n";std::cout << " sell_price: " << stocks_[i].sell_price << "\n";std::cout << " sell_quantity: " << stocks_[i].sell_quantity << "\n";}}else{// An error occurred.std::cerr << e.message() << std::endl;}// Since we are not starting a new operation the io_context will run out of// work to do and the client will exit.}private:/// The connection to the server.connection connection_;/// The data received from the server.std::vector<stock> stocks_;};} // namespace s11n_exampleint 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_context io_context;s11n_example::client client(io_context, argv[1], argv[2]);io_context.run();}catch (std::exception& e){std::cerr << e.what() << std::endl;}return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。