Asio
lastmod 2020-07-24created 2015-03-1428 edits
Asio is a freely available, open source, cross-platform C++ library for network programming. It provides developers with a consistent asynchronous I/O model using a modern C++ approach. Boost.Asio was accepted into the Boost library on 30 December 2005 after a 20-day review. The library has been developed by Christopher M. Kohlhoff since 2003. A networking proposal based on Asio was submitted to the C++ standards committee in 2006 for possible inclusion in the second Technical Report on C++ Library Extensions (TR2).
How to compile
To use the asio library on MinGW, you must add the -lWS2_32 option1.
On MinGW, if a message like the one below is printed, add -lmswsock to the linker options.
Below is the compile & link method used with Linux G++.
g++ -o echo-server.o -c echo-server.cpp -std=c++11
g++ -o echo-server echo-server.o -lboost_system -lpthread
Below is the compile & link method actually used on MinGW.
g++ -o echo-server.o -c echo-server.cpp -std=c++11 -DWINDOWS
g++ -o echo-server echo-server.o -lboost_system.dll -lWS2_32 -lmswsock
In addition, a warning like the following may be printed.
C:\workspace\root\var\opm\local\include/boost/asio/detail/config.hpp:551:5: warning: #warning Please
define _WIN32_WINNT or _WIN32_WINDOWS appropriately. [-Wcpp]
In this case, add the Windows-related defines as shown below.
// config.h
#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif
#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif
// Minimum Platform version.
#ifndef WINVER
#define WINVER _WIN32_WINNT
#endif
// Minimum Internet Explorer version.
#ifndef _WIN32_IE
#define _WIN32_IE 0x0700
#endif
#endif // WINDOWS
boost::asio::io_service
The io_service class provides the core I/O functionality for users of the asynchronous I/O objects, including:
- boost::asio::ip::tcp::socket
- boost::asio::ip::tcp::acceptor
- boost::asio::ip::udp::socket
- boost::asio::deadline_timer
I/O Services and I/O Objects
Asynchronous data processing is based on I/O services and I/O objects. I/O services abstract the operating system interfaces that enable asynchronous processing, and I/O objects are used to start specific asynchronous operations. The boost::io_service class is the class for I/O services, and it is implemented with optimizations for each operating system.
Boost.Asio has several classes for each I/O Object. Among them, boost::asio::ip::tcp::socket handles network sending and receiving, and boost::asio::deadline_timer provides timer functionality.
Server & Client
The communication flow between server and client is as follows.
| Server | Client | Brief |
| | Create the service object | |
| | | Create an endpoint object to set the IP Address & Port Number. |
| | Attach the io_service to the socket. | |
| | Create the Acceptor that will connect client sockets, then attach the io_service and endpoint. | |
| | Prepare for client connections. | |
| | Connect to the server. | |
| | Read & write. | |
| | Close the socket. | |
Error code
- [Recommended] asio changes the error code before returning it 2
- System Error 955 when using resolver::async_resolve
Explanations of error codes that can occur while using ASIO.
Simple example
- C++ network library asio
- Example: Asio_example_130327.zip
Most operations work by passing a handler to a function as an argument; the handler is called when the work completes.
// Object initialize:
asio::io_service io_service;
tcp::socket _socket = new tcp::socket(io_service);
// ACCEPT
tcp::acceptor _acceptor;
_acceptor.async_accept( _acceptSocket, handler );
// SEND
asio::async_write( _socket, asio::buffer(buffer, len), handler );
// RECEIVE
_socket->async_read_some( asio::buffer(buffer, len), handler );
Async Echo Server/Client
The asynchronous ECHO server and client code can be found here.
UDS Server/Client
Sample code using UDS can be found here.
Documentation
- Naver laster40 blog - Boost ASIO Tutorials
- http://blog.naver.com/laster40/49785570
-
Blog.naver.com-laster40-boost-asio.zip - Features of ASIO.
- ASIO endpoint & address.
- ASIO tutorial analysis (1).
- ASIO tutorial analysis (2) A synchronous TCP daytime server.
- ASIO tutorial analysis (3) A synchronous TCP daytime client.
- ASIO tutorial analysis (4) An asynchronous TCP daytime server.
- ASIO example ( 1 ) - Custom Allocator.
- ASIO example ( 2 ) - Reference-counted Buffer.
- ASIO thread model ( 1 ) - single thread model.
- ASIO thread model ( 2 ) - multi thread model - io_service Per Thread.
- ASIO thread model ( 3 ) - multi thread - single io_service.
- Building a NetEngine with ASIO ( 1 ) - Requirements.
See also
Favorite site
- asio web site
- Wikipedia (en) Description of ASIO
- Stackoverflow: How does libuv compare to Boost/ASIO? 3
Documentation
Tutorials
- boost::asio - Korean manual for the asynchronous socket I/O classes 5
- GET BOOST - Translation of the tutorial for asio, newly added in Boost 1.35 6
- Boost Asio Korean tutorial 7
Guide
- [Recommended] boost - asio, Asynchronous Input and Output 8, 9
- [Recommended] ( Boost ) boost::asio Part1.
- boost asio Part1, 2
-
http://blog.daum.net/aswip/8429366 - boost::asio asynchronous server/client step 1 10
- Timer.1 - Using a timer synchronously
- How to ensure io_service always keeps running
- [Recommended] Asynchronous echo server based on Boost.Asio 11
- Network programming with Boost.Asio (PDF)
Example
- [Recommended] Boost.asio Examples
- Stackoverflow: Audio streaming using C++ tutorial and sample code
- Github: A simple boost asio based mjpeg streaming server
- Boost.Asio with Protocol Buffers code sample (Protobuf)
Tip
- Structural problems of boost::asio (timeout issues, etc.)
- Things to watch out for in asynchronous processing with the boost::asio library - strand
References
-
Links WS2_32.lib. ↩
-
Boost_asio_error_code_0_error.pdf ↩
-
How_does_libuv_compare_to_Boost_ASIO_-_Stack_Overflow.pdf ↩
-
Asioref-1.10.2.pdf ↩
-
Boost.asio-1.35-ko.pdf ↩
-
BoostAsio_1.35_-tutorial-_ko.doc.zip ↩
-
Boost_asio.pdf ↩
-
Jeremyko.blogspot.kr_-boost-_Asynchronous_Input_and_Output.pdf ↩
-
Boost_-_asio_Asynchronous_Input_and_Output.pdf ↩
-
Boost_asio_asynchronous_server_client_step1.pdf ↩
-
Asio_asynchronous_echo_server.pdf ↩