[Boost] ASIO explanation of boost Library 8-a few simple examples of TCP

Source: Internet
Author: User
Tags echo 7

I have made some minor changes and made notes on several examples on the boost official website.

Synchronization Client
Void test_asio_synclient () {typedef boost: ASIO: io_service ioservice; // The namespace has several common classes: accetpt, resolver, endpoint, sockettypedef boost: ASIO :: IP: TCP; try {ioservice Ios; Boost: System: error_code error; // 1. the interpreter is often used to resolve domain names, such as // query ("www.163.com", "80"), or query ("www.163.com", "Telnet ") // Echo 7/tcp // FTP 21/tcp # File Transfer Protocol (Control) // Telnet 23/tcp # virtual terminal protocol/s MTP 25/tcp # Simple Mail Transfer Protocol // time 37/tcp timeserver # Time // TCP: resolver (IOS); // TCP: resolver :: query query ("127.0.0.1", "10000"); // TCP: resolver: iterator endpoint_iterator = resolver. resolve (query); // TCP: resolver: iterator end; // TCP: Socket socket (IOS); // socket. connect (* endpoint_iterator, error); // 2. A Simple Method for TCP: Socket socket (IOS); TCP: endpoint (boost: ASI O: IP: address_v4: from_string ("127.0.0.1"), 10000); socket. connect (endpoint, error); // you need to judge here; otherwise, the connection fails. if (error) Throw boost: System: system_error (error); While (true) {// boost: array <char, 128> Buf; STD :: vector <char> Buf (128); size_t Len = socket. read_some (boost: ASIO: buffer (BUF), error); // you need to judge this. Otherwise, the server is disconnected and an endless loop occurs. if (error = boost: ASIO: Error: EOF) break; // Connection closed cleanly by P Eer. Else if (error) Throw boost: System: system_error (error); // some other error. // pay attention to the size of the Buf. Socket. write_some (boost: ASIO: buffer (BUF, Len), error) ;}} catch (STD: exception & E) {print_debug (E. what ());}}
Synchronous Server
Namespace {STD: String make_daytime_string () {using namespace STD; // For time_t, time and ctime; time_t now = time (0); Return ctime (& now );}} void test_asio_synserver () {typedef boost: ASIO: io_service ioservice; typedef boost: ASIO: IP: TCP; try {ioservice Ios; /** the following constructor is equivalent to * basic_socket_acceptor <TCP> acceptor (io_service); * acceptor. open (TCP: V4 (); * acceptor. BIND (TCP: endpoint (13); * acceptor. listen (0); // default * // * TCP: The endpoint is composed of three parts: 1. IP address (address, address_v4, address_v6) 2. port Number 3. protocol version */tcp: acceptor (IOS, TCP: endpoint (TCP: V4 (), 13); For (;) {tcp :: socket socket (IOS); acceptor. accept (socket); STD: String message = make_daytime_string (); Boost: System: error_code ignored_error; Boost: ASIO: Write (socket, boost: ASIO :: buffer (Message), boost: ASIO: transfer_all (), ignored_error); print_debug (Message) ;}} catch (STD: exception & E) {STD :: cout <E. what () <STD: Endl ;}}
Asynchronous Server
Namespace {typedef boost: ASIO: io_service ioservice; typedef boost: ASIO: IP: TCP; STD: String make_daytime_string () {using namespace STD; time_t now = STD: Time (null); Return ctime (& now);} class tcp_connection: Public boost: enable_shared_from_this <tcp_connection> {public: typedef boost :: shared_ptr <tcp_connection> pointer; static pointer create (ioservice & io_service) {return pointer (New tcp_connection (I O_service);} TCP: Socket & socket () {return socket _;} void start () {message _ = make_daytime_string (); Boost: ASIO :: async_write (socket _, boost: ASIO: buffer (Message _), boost: BIND (& tcp_connection: handle_write, shared_from_this (), boost: ASIO: placeholders:: error, boost: ASIO: placeholders: bytes_transferred);} PRIVATE: tcp_connection (ioservice & io_service): Socket _ (io_service) {} void handle_write (const B Oost: System: error_code &/* Error */, size_t/* bytes_transferred */) {print_debug ("Write Data !!! ");} TCP: Socket socket _; STD: String message _;}; Class tcp_server {public: tcp_server (ioservice & io_service): acceptor _ (io_service, TCP :: endpoint (TCP: V4 (), 10000) {start_accept ();} PRIVATE: void start_accept () {tcp_connection: pointer new_connection = tcp_connection: Create (acceptor _. get_io_service (); acceptor _. async_accept (new_connection-> socket (), boost: BIND (& tcp_server: handle_accept, this, new_connec Tion, boost: ASIO: placeholders: Error);} void handle_accept (tcp_connection: pointer new_connection, const boost: System: error_code & error) {If (! Error) {new_connection-> Start (); start_accept () ;}} TCP: acceptor _ ;}/// void test_asio_asynserver () {try {ioservice io_service; tcp_server server (io_service); // The callback object will be called only after the run () method of the io_service class is run. io_service.run ();} catch (STD :: exception & E) {STD: cerr <E. what () <STD: Endl ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.