http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/
Compilation Environment boost1.59 vs2015
A Guide to Getting Started with Boost::asio
Boost ASIO learning Guide
Boost::asio is a cross-platform C + + network that provides developers with asynchronous models using modern C + + methods and
The underlying IO library. Now has a large number of users and become part of the boost library
Before we begin, we will look at the following Boost::asio review. It includes a lot of basic things that we have to know
Information. The guide will not cover the complex details of the entire boost library, he will guide us in learning how to
Use ASIO in the sequence.
This guide does not teach any programming language. Readers must be familiar with C + + and be proficient in using boost libraries, especially ASIO libraries. The guide also does not teach network programming. In other words, the goal of this guide is to instruct readers to use ASIO, but still require readers to spend time reading official documents.
The example in this guide is compiled under Windows. If your code needs to be ported, you may need to make some logical changes. All code has been compressed and uploaded, does not contain engineering files, only contains source files. The code relies on the boost library.
1 Basics of Io_service
The core object of Boost::asio is Io_service. This object is similar to the library's brain and heart. We use a simple example to familiarize ourselves with it and call the run member function. If you look at the document description of the function, the run function blocks until all tasks are complete and no processors are distributed, or until Io_service is stopped.
code example 1a
#include <boost/asio.hpp> #include <iostream>int main (int argc, char * argv[]) {Boost::asio::io_service Io_ Service;io_service.run () std::cout << "Do you reckon the line displays?" << Std::endl;return 0;}
Based on the description of the document, we expect that the text will be printed. When we run the program, we get the expected effect, and we see this line of text.
We will create a task class that notifies io_service when a task is running. In other words, when Io_service has a task object associated with it, it will not stop running. The next example validates this assertion.
code example 1b
#include <boost/asio.hpp> #include <iostream>int main (int argc, char * argv[]) {Boost::asio::io_service Io_ Service;boost::asio::io_service::work work (Io_service); Io_service.run () std::cout << "does you reckon the line Displays? "<< Std::endl;return 0;}
Boost ASIO translation (i)