Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

Daytime.2 - A synchronous TCP daytime server

This tutorial program shows how to use asio to implement a server application with TCP.

#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

We define the function make_daytime_string() to create the string to be sent back to the client. This function will be reused in all of our daytime server applications.

A ip::tcp::acceptor object needs to be created to listen for new connections. It is initialised to listen on TCP port 13, for IP version 4.

This is an iterative server, which means that it will handle one connection at a time. Create a socket that will represent the connection to the client, and then wait for a connection.

A client is accessing our service. Determine the current time and transfer this information to the client.

Finally, handle any exceptions.

See the full source listing

Return to the tutorial index

Previous: Daytime.1 - A synchronous TCP daytime client

Next: Daytime.3 - An asynchronous TCP daytime server


PrevUpHomeNext