| Home | Libraries | People | FAQ | More

# services/daytime_client.cpp

Go to the documentation of this file.

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

| Copyright © 2003 - 2006 Christopher M. Kohlhoff