简体   繁体   English

增强Asio多线程TCP同步服务器

[英]Boost Asio Multithreaded TCP Synchronous Server

I am trying to create a tcp synchronous server. 我正在尝试创建一个tcp同步服务器。 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? 我甚至需要一个线程池吗?

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 无需在上下文中调用io_service::run() ,请参阅文档

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. run()函数将阻塞,直到所有工作完成,并且不再有调度程序,或者直到io_service已停止。

You haven't added any handlers to the io_service so there is no need to invoke run() . 您尚未向io_service添加任何处理程序,因此无需调用run() If you use asynchronous methods such as async_accept() , then you will need to run() the io_service . 如果使用异步方法(如async_accept() ,则需要run() io_service

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 这并没有直接帮助,但它会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM