Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Asio Multithreaded TCP Synchronous Server

I am trying to create a tcp synchronous server. My main thread would create listen to a port, and an incoming connection would be handled by a thread.

My code:

void WorkerThread(boost::shared_ptr< boost::asio::io_service >  io_service)
{
    io_service->run();
}

void Application::server()
{
        boost::shared_ptr< boost::asio::io_service > io(
            new boost::asio::io_service()
            );
        boost::shared_ptr< boost::asio::io_service::work > work(
            new boost::asio::io_service::work(*io)
            );
        // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR
        boost::asio::ip::tcp::acceptor acceptor(*io);
        boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 2198);
        acceptor.open(endpoint.protocol());
        acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
        acceptor.bind(endpoint);
        acceptor.listen();              

        // pool of threads
        boost::thread_group worker_threads;
        for(int x = 0; x < 5; ++x)
        {
            worker_threads.create_thread(boost::bind(&WorkerThread, io));
        }

        while(true)
        {
            boost::shared_ptr< boost::asio::ip::tcp::socket > socket(
                new boost::asio::ip::tcp::socket( *io )
                );
            acceptor.accept(*socket);
            processConnection(*socket);
            socket->close();
        }

        io->stop();
        worker_threads.join_all();

}

void Application::processConnection(boost::asio::ip::tcp::socket & socket)
{
    boost::asio::streambuf request_buffer;
    std::istream request_stream(&request_buffer);
    // repsonse buffer
    boost::asio::streambuf response_buffer;
    std::ostream response_stream(&response_buffer); 
    boost::asio::read_until(socket, request_buffer, "</message>"); 

    // process request_buffer into response_buffer

    boost::asio::write(socket, response_buffer);

}

The following is working with more than one client connecting to the server; however, it also work if I remove the pool of thread. Can anyone explain me why that is? Do I even need a pool of threads?

like image 381
Ivan Santos Avatar asked Nov 14 '11 19:11

Ivan Santos


2 Answers

however, it also work if I remove the pool of thread. Can anyone explain me why that is? Do I even need a pool of threads?

You do not need a pool of threads given your sample code. There is no need to invoke io_service::run() in your context, see the documentation

The run() function blocks until all work has finished and there are no more handlers to be dispatched, or until the io_service has been stopped.

You haven't added any handlers to the io_service so there is no need to invoke run(). If you use asynchronous methods such as async_accept(), then you will need to run() the io_service.

like image 91
Sam Miller Avatar answered Oct 25 '22 08:10

Sam Miller


you might find it easier to read your code if you typedef the boost stuff

example

typedef boost::asio::ip::tcp::socket TSocket;

This does not directly help but it will help

like image 39
gda2004 Avatar answered Oct 25 '22 09:10

gda2004