asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design
Examples

serialization/client.cpp

Go to the documentation of this file.
00001 //
00002 // client.cpp
00003 // ~~~~~~~~~~
00004 //
00005 // Copyright (c) 2003-2007 Christopher M. Kohlhoff (chris at kohlhoff dot com)
00006 //
00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
00009 //
00010 
00011 #include <asio.hpp>
00012 #include <boost/bind.hpp>
00013 #include <iostream>
00014 #include <vector>
00015 #include "connection.hpp" // Must come before boost/serialization headers.
00016 #include <boost/serialization/vector.hpp>
00017 #include "stock.hpp"
00018 
00019 namespace s11n_example {
00020 
00022 class client
00023 {
00024 public:
00026   client(asio::io_service& io_service,
00027       const std::string& host, const std::string& service)
00028     : connection_(io_service)
00029   {
00030     // Resolve the host name into an IP address.
00031     asio::ip::tcp::resolver resolver(io_service);
00032     asio::ip::tcp::resolver::query query(host, service);
00033     asio::ip::tcp::resolver::iterator endpoint_iterator =
00034       resolver.resolve(query);
00035     asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00036 
00037     // Start an asynchronous connect operation.
00038     connection_.socket().async_connect(endpoint,
00039         boost::bind(&client::handle_connect, this,
00040           asio::placeholders::error, ++endpoint_iterator));
00041   }
00042 
00044   void handle_connect(const asio::error_code& e,
00045       asio::ip::tcp::resolver::iterator endpoint_iterator)
00046   {
00047     if (!e)
00048     {
00049       // Successfully established connection. Start operation to read the list
00050       // of stocks. The connection::async_read() function will automatically
00051       // decode the data that is read from the underlying socket.
00052       connection_.async_read(stocks_,
00053           boost::bind(&client::handle_read, this,
00054             asio::placeholders::error));
00055     }
00056     else if (endpoint_iterator != asio::ip::tcp::resolver::iterator())
00057     {
00058       // Try the next endpoint.
00059       connection_.socket().close();
00060       asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00061       connection_.socket().async_connect(endpoint,
00062           boost::bind(&client::handle_connect, this,
00063             asio::placeholders::error, ++endpoint_iterator));
00064     }
00065     else
00066     {
00067       // An error occurred. Log it and return. Since we are not starting a new
00068       // operation the io_service will run out of work to do and the client will
00069       // exit.
00070       std::cerr << e.message() << std::endl;
00071     }
00072   }
00073 
00075   void handle_read(const asio::error_code& e)
00076   {
00077     if (!e)
00078     {
00079       // Print out the data that was received.
00080       for (std::size_t i = 0; i < stocks_.size(); ++i)
00081       {
00082         std::cout << "Stock number " << i << "\n";
00083         std::cout << "  code: " << stocks_[i].code << "\n";
00084         std::cout << "  name: " << stocks_[i].name << "\n";
00085         std::cout << "  open_price: " << stocks_[i].open_price << "\n";
00086         std::cout << "  high_price: " << stocks_[i].high_price << "\n";
00087         std::cout << "  low_price: " << stocks_[i].low_price << "\n";
00088         std::cout << "  last_price: " << stocks_[i].last_price << "\n";
00089         std::cout << "  buy_price: " << stocks_[i].buy_price << "\n";
00090         std::cout << "  buy_quantity: " << stocks_[i].buy_quantity << "\n";
00091         std::cout << "  sell_price: " << stocks_[i].sell_price << "\n";
00092         std::cout << "  sell_quantity: " << stocks_[i].sell_quantity << "\n";
00093       }
00094     }
00095     else
00096     {
00097       // An error occurred.
00098       std::cerr << e.message() << std::endl;
00099     }
00100 
00101     // Since we are not starting a new operation the io_service will run out of
00102     // work to do and the client will exit.
00103   }
00104 
00105 private:
00107   connection connection_;
00108 
00110   std::vector<stock> stocks_;
00111 };
00112 
00113 } // namespace s11n_example
00114 
00115 int main(int argc, char* argv[])
00116 {
00117   try
00118   {
00119     // Check command line arguments.
00120     if (argc != 3)
00121     {
00122       std::cerr << "Usage: client <host> <port>" << std::endl;
00123       return 1;
00124     }
00125 
00126     asio::io_service io_service;
00127     s11n_example::client client(io_service, argv[1], argv[2]);
00128     io_service.run();
00129   }
00130   catch (std::exception& e)
00131   {
00132     std::cerr << e.what() << std::endl;
00133   }
00134 
00135   return 0;
00136 }
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design