Boost ASIO Basic Concepts

Source: Internet
Author: User

Based on the asynchronous mechanism provided by the operating system , the ASIO library implements portable asynchronous (or synchronous) IO operations using the proactive mode (proactor), eliminating the need for multithreading and locking, effectively avoiding many harmful side effects (such as competition, deadlock) caused by multithreaded programming.

ASIO encapsulates the operating system's Select, Kqueue, Poll/epoll, overlapped I/O mechanisms to implement the asynchronous IO model. In synchronous mode, the program initiates an IO operation, submits the request to the Io_service, Io_service the operation to the operating system, and waits synchronously. When the IO operation is complete, the operating system notifies Io_service, and then Io_service sends the results back to the program to complete the synchronization process. In asynchronous mode, in addition to initiating IO operations, the program defines a completion handler for the callback . The Io_service also forwards the IO operation to the operating system, but it does not wait synchronously, but returns immediately. The run () member function that calls Io_service can wait for the asynchronous operation to complete, Io_service obtains the result from the operating system when the asynchronous operation completes, and performs subsequent logic on the call handler.

1. Handler

callback function form:

void Handler (const Error_code &EC); void Handler (const error_code &ec, Std::size_type bytes_transferred);

2. Io_service

The Io_service class represents an asynchronous processing mechanism (such as epoll) in the system, must be initialized before other objects in the ASIO library, and other objects commit asynchronous operations handler to Io_service.

3. Const_buffer

A data buffer that holds a void* memory address and data length.

4. Network communication related

  The ASIO supports TCP, UDP, and ICMP communication protocols, which encapsulate the Berkeley socket API well. The TCP section defines some typedef types for TCP communication, including endpoint class endpoint, socket class socket, stream class iostream, receiver acceptor, parser resolver, and so on. 

classtcp{ Public:  ///The type of a TCP endpoint.typedef basic_endpoint<tcp>endpoint; ///The TCP socket type.typedef basic_stream_socket<tcp>socket; ///The TCP acceptor type.typedef basic_socket_acceptor<tcp>acceptor; ///The TCP resolver type.typedef basic_resolver<tcp>Resolver; ///The TCP iostream type.typedef basic_socket_iostream<tcp>iostream; /// ......};

Endpoint contains the IP address and the port number used for the communication.

The socket can specify the protocol or endpoint to use at construction time, or call the member function connect () later. After successful connection, you can use Local_endpoint () and Remote_endpoint () to obtain the endpoint information of both ends of the connection, get the number of bytes that can be read with available (), receive ()/read_some () and send ()/ Write_some () reads and writes data, and closes the socket using the close () function when the operation is complete. If the socket is not closed, its destruction is also automatically turned off.

The acceptor corresponds to the socket API's accept () function for the server side.

Boost ASIO Basic Concepts

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.