Book Image

Boost.Asio C++ Network Programming Cookbook

By : Dmytro Radchuk
Book Image

Boost.Asio C++ Network Programming Cookbook

By: Dmytro Radchuk

Overview of this book

Starting with recipes demonstrating the execution of basic Boost.Asio operations, the book goes on to provide ready-to-use implementations of client and server applications from simple synchronous ones to powerful multithreaded scalable solutions. Finally, you are presented with advanced topics such as implementing a chat application, implementing an HTTP client, and adding SSL support. All the samples presented in the book are ready to be used in real projects just out of the box. As well as excellent practical examples, the book also includes extended supportive theoretical material on distributed application design and construction.
Table of Contents (13 chapters)
Boost.Asio C++ Network Programming Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Implementing an asynchronous TCP server


An asynchronous TCP server is a part of a distributed application that satisfies the following criteria:

  • Acts as a server in the client-server communication model

  • Communicates with client applications over TCP protocol

  • Uses the asynchronous I/O and control operations

  • May handle multiple clients simultaneously

A typical asynchronous TCP server works according to the following algorithm:

  1. Allocate an acceptor socket and bind it to a particular TCP port.

  2. Initiate the asynchronous accept operation.

  3. Spawn one or more threads of control and add them to the pool of threads that run the Boost.Asio event loop.

  4. When the asynchronous accept operation completes, initiate a new one to accept the next connection request.

  5. Initiate the asynchronous reading operation to read the request from the connected client.

  6. When the asynchronous reading operation completes, process the request and prepare the response message.

  7. Initiate the asynchronous writing operation to send the response...