| Home | Libraries | People | FAQ | More

# services/daytime_client.cpp File Reference

Go to the source code of this file.

## Typedefs

typedef boost::asio::basic_stream_socket< boost::asio::ip::tcp, services::stream_socket_service< boost::asio::ip::tcp > > | debug_stream_socket

## Functions

void | read_handler (const boost::asio::error &e, std::size_t bytes_transferred, debug_stream_socket *s)
void | connect_handler (const boost::asio::error &e, debug_stream_socket *s, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
int | main (int argc, char *argv[])

## Variables

char | read_buffer [1024]

## Typedef Documentation

typedef boost::asio::basic_stream_socket<boost::asio::ip::tcp, services::stream_socket_service<boost::asio::ip::tcp> > debug_stream_socket

Definition at line 8 of file daytime_client.cpp.

## Function Documentation

void read_handler | ( | const boost::asio::error & | e,
|  | std::size_t | bytes_transferred,
|  | debug_stream_socket * | s |
| ) |  |  |

Definition at line 12 of file daytime_client.cpp.

Referenced by connect_handler().

    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 }

void connect_handler | ( | const boost::asio::error & | e,
|  | debug_stream_socket * | s,
|  | boost::asio::ip::tcp::resolver::iterator | endpoint_iterator |
| ) |  |  |

Definition at line 25 of file daytime_client.cpp.

Referenced by main().

    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 }

int main | ( | int | argc,
|  | char * | argv[] |
| ) |  |  |

Definition at line 48 of file daytime_client.cpp.

    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 }

## Variable Documentation

char read_buffer[1024]

Definition at line 10 of file daytime_client.cpp.

Referenced by connect_handler(), and read_handler().

| Copyright © 2003 - 2006 Christopher M. Kohlhoff