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

Examples |

# services/daytime_client.cpp File Reference

Go to the source code of this file.

## Typedefs

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

## Functions

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

## Variables

char | read_buffer [1024]

## Typedef Documentation

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

Definition at line 18 of file daytime_client.cpp.

## Function Documentation

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

Definition at line 22 of file daytime_client.cpp.

Referenced by connect_handler().

    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 }

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

Definition at line 35 of file daytime_client.cpp.

Referenced by main().

    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 }

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

Definition at line 58 of file daytime_client.cpp.

    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 }

## Variable Documentation

char read_buffer[1024]

Definition at line 20 of file daytime_client.cpp.

Referenced by connect_handler(), and read_handler().

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