Pages

Minimal ASIO TCP server

Here we are going to write a minimal synchronous TCP "Hello" server that is meant to say hello to the first client that asks for it, and then terminate is execution. It is not very useful, but it should be a good way to get introduced to the Boost ASIO library.

In an ASIO program, we instantiate an io_service object, and than we use it to get the services we require.

This post is ancient (February 2011), please follow the link to its newer version I have written on March 2018. Thank you.


To create an TCP/IP server, we have to specify an endpoint - identified by the used protocol (say, TCP/IP version 4) and a port (I picked up almost randomly 50013 - you should use a port not already used by some other application on you target machine) - and then use the io_service and the endpoint to create an acceptor - an object that we'll use to rule the access on the socket.

Before putting the socket on wait for a client request, we actually have to create the socket itself, using again the io_service as reference.

Here is the code:
#include <iostream>
#include <string>
#include <boost/asio.hpp>

namespace
{
    const int HELLO_PORT = 50013; // 1
}

int main()
{
    try
    {
        boost::asio::io_service io_service;
        boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), HELLO_PORT);
        boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint);
        boost::asio::ip::tcp::socket socket(io_service);

        std::cout << "Server ready" << std::endl; // 2
        // just once
        {
            acceptor.accept(socket); // 3

            std::string message("Hello from server\n");
            boost::asio::write(socket, boost::asio::buffer(message)); // 4
            socket.close(); // 5
        }
    }
    catch(std::exception& e) // 6
    {
        std::cerr << "Exception: " << e.what() << std::endl;
    }
}
1. The port used by our Hello Server
2. We have done all the initialization job. A socket has been created on the asio I/O service, and a tcp acceptor keeps together the I/O service and to the specified endpoint.
3. We pass the socket to the acceptor by its accept() method. What happens is that we hang waiting for a client to establish a connection.
4. We have a client-server connection. Now we send a message ot the socket. Since the socket keeps the connection to the client, we expect the client to get this message.
5. Here it's pretty useless to close the socket, since we are about to discard it. But we can easily change the code, making this block an infinite loop, and in this case this call is essential to reuse the socket for the next connection.
6. Any problem, we just signal the issue and terminate.

Post based on a page from the official Boost ASIO tutorial.

2 comments: