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

services/daytime_client.cpp

Go to the documentation of this file.
00001 //
00002 // daytime_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 "logger.hpp"
00015 #include "stream_socket_service.hpp"
00016 
00017 typedef asio::basic_stream_socket<asio::ip::tcp,
00018     services::stream_socket_service<asio::ip::tcp> > debug_stream_socket;
00019 
00020 char read_buffer[1024];
00021 
00022 void read_handler(const asio::error_code& e,
00023     std::size_t bytes_transferred, debug_stream_socket* s)
00024 {
00025   if (!e)
00026   {
00027     std::cout.write(read_buffer, bytes_transferred);
00028 
00029     s->async_read_some(asio::buffer(read_buffer),
00030         boost::bind(read_handler, asio::placeholders::error,
00031           asio::placeholders::bytes_transferred, s));
00032   }
00033 }
00034 
00035 void connect_handler(const asio::error_code& e, debug_stream_socket* s,
00036     asio::ip::tcp::resolver::iterator endpoint_iterator)
00037 {
00038   if (!e)
00039   {
00040     s->async_read_some(asio::buffer(read_buffer),
00041         boost::bind(read_handler, asio::placeholders::error,
00042           asio::placeholders::bytes_transferred, s));
00043   }
00044   else if (endpoint_iterator != asio::ip::tcp::resolver::iterator())
00045   {
00046     s->close();
00047     asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
00048     s->async_connect(endpoint,
00049         boost::bind(connect_handler,
00050           asio::placeholders::error, s, ++endpoint_iterator));
00051   }
00052   else
00053   {
00054     std::cerr << e.message() << std::endl;
00055   }
00056 }
00057 
00058 int main(int argc, char* argv[])
00059 {
00060   try
00061   {
00062     if (argc != 2)
00063     {
00064       std::cerr << "Usage: daytime_client <host>" << std::endl;
00065       return 1;
00066     }
00067 
00068     asio::io_service io_service;
00069 
00070     // Set the name of the file that all logger instances will use.
00071     services::logger logger(io_service, "");
00072     logger.use_file("log.txt");
00073 
00074     // Resolve the address corresponding to the given host.
00075     asio::ip::tcp::resolver resolver(io_service);
00076     asio::ip::tcp::resolver::query query(argv[1], "daytime");
00077     asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
00078     asio::ip::tcp::endpoint endpoint = *iterator;
00079 
00080     // Start an asynchronous connect.
00081     debug_stream_socket socket(io_service);
00082     socket.async_connect(endpoint,
00083         boost::bind(connect_handler,
00084           asio::placeholders::error, &socket, ++iterator));
00085 
00086     // Run the io_service until all operations have finished.
00087     io_service.run();
00088   }
00089   catch (std::exception& e)
00090   {
00091     std::cerr << e.what() << std::endl;
00092   }
00093 
00094   return 0;
00095 }
asio 0.3.8rc3 Home | Reference | Tutorial | Examples | Design