Boost::asio Getting Started anatomy

Source: Internet
Author: User

Boost::asio can perform synchronous or asynchronous operations on I/O objects such as sockets, it is necessary to understand Boost::asio, your programs, and the process of their interaction before using Boost::asio. As an example of a bootstrap, we think about what happens when a socket performs a connection operation, and we start with a synchronous example where your program needs a Io_service object, and Io_service links your program to the OS I/O device. Boost::asio::io_service Io_service;

Your program requires an I/O object to perform I/O operations, such as a TCP socketboost::asio::ip::tcp::socket socket (io_service);

The following events occur sequentially when a synchronous connection operation is performed

1. Your program initiates the connection operation by invoking the I/O object Socket.connect (server_endpoint);

2.I/O object forwarding request to Io_service

3.io_service notifies the operating system to perform a connection operation

4. The operating system returns the results of the connection operation to the Io_service

5.io_service the error of the operation into Boost::system::error_code, a error_code may be compared with a specific value, or as a boolen (false result means that no error occurred), the result is transmitted i/ O object.

6. If the operation fails the I/O object throws an exception Boost::system::error_code, if the start operation is replaced with the following Boost::system::error_code Ec;socket.connect (Server_ Endpoint, EC); Then the result of the operation is written to the Error_code type variable EC, and no exception is returned when an asynchronous operation is executed and another different sequence of events 1. Your program initiates a connection operation by invoking an I/O object socket.async_ Connect (server_endpoint, your_completion_handler); Your_completion_handler is a function or a named function object void Your_completion_ Handler (const boost::system::error_code& EC); This function object relies on the asynchronous operation being performed, and each operation is started in the appropriate manner

The 2.I/O object passes the request to Io_service

3.io_service signaling notifies the operating system to initiate an asynchronous connection

Time passes (synchronous operation will wait here)

4. The operating system puts the results of the operation in a queue to indicate that the connection is starting to complete, for Io_service to extract the results

5. Your program gets the result of the operation by calling Io_service::run () (get some other similar io_service member function), if there is an incomplete asynchronous operation Io_service::run () will block, You must call it periodically after initiating your asynchronous operation.

6. Inside Io_service::run (), io_service the result of the operation and sends it to Your_completion_handler, which is a brief flow of Boost::asio work, if you want to understand advanced functions, such as boost :: ASIO extends to other types of asynchronous operations that you need to understand more deeply.

Boost::asio Getting Started anatomy

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.