Skip to content
Search

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.

undefined reference to `AcceptEx@32' ...

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

boost::asio::io_service

Create the service object

boost::asio::ip::tcp::endpoint
boost::asio::ip::tcp::v4()

boost::asio::ip::tcp::endpoint
boost::asio::ip::tcp::address::from_string()

Create an endpoint object to set the IP Address & Port Number.

boost::asio::ip::tcp::socket

Attach the io_service to the socket.

boost::asio::ip::tcp::acceptor

Create the Acceptor that will connect client sockets, then attach the io_service and endpoint.

acceptor.accept(socket);

Prepare for client connections.

socket.connect(endpoint);

Connect to the server.

socket.write_some()
socket.async_write_some()
socket.read_some()
socket.async_read_some()

Read & write.

socket.close();

Close the socket.

Error code

Explanations of error codes that can occur while using ASIO.

Simple example

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

Documentation

Tutorials

Guide

Example

Tip

References


  1. Links WS2_32.lib. ↩

  2. Boost_asio_error_code_0_error.pdf ↩

  3. How_does_libuv_compare_to_Boost_ASIO_-_Stack_Overflow.pdf ↩

  4. Asioref-1.10.2.pdf ↩

  5. Boost.asio-1.35-ko.pdf ↩

  6. BoostAsio_1.35_-tutorial-_ko.doc.zip ↩

  7. Boost_asio.pdf ↩

  8. Jeremyko.blogspot.kr_-boost-_Asynchronous_Input_and_Output.pdf ↩

  9. Boost_-_asio_Asynchronous_Input_and_Output.pdf ↩

  10. Boost_asio_asynchronous_server_client_step1.pdf ↩

  11. Asio_asynchronous_echo_server.pdf ↩

author Zer0 sitemap