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 a synchronous iterative TCP server


A synchronous iterative 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 I/O and control operations that block the thread of execution until the corresponding operation completes, or an error occurs

  • Handles clients in a serial, one-by-one fashion

A typical synchronous iterative TCP server works according to the following algorithm:

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

  2. Run a loop until the server is stopped:

    1. Wait for the connection request from a client.

    2. Accept the client's connection request when one arrives.

    3. Wait for the request message from the client.

    4. Read the request message.

    5. Process the request.

    6. Send the response message to the client.

    7. Close the connection with the client and deallocate the socket.

This recipe demonstrates how to implement a synchronous iterative TCP...