Boost Asio 多线程 TCP 同步服务器

发布于 2024-12-15 05:34:23 字数 2042 浏览 0 评论 0原文

我正在尝试创建一个 tcp 同步服务器。我的主线程将创建侦听端口,并且传入连接将由线程处理。

我的代码:

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);

}

以下是与连接到服务器的多个客户端一起使用的;但是,如果我删除线程池,它也可以工作。谁能解释一下这是为什么吗?我还需要一个线程池吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呢古 2024-12-22 05:34:23

但是,如果我删除线程池,它也可以工作。任何人都可以
解释一下这是为什么?我还需要一个线程池吗?

给定示例代码,您不需要线程池。无需在您的上下文中调用 io_service::run() ,请参阅 文档

run() 函数会阻塞,直到所有工作完成并且没有
更多处理程序将被调度,或者直到 io_service 被调度
停止了。

您尚未向 io_service 添加任何处理程序,因此无需调用 run()。如果您使用异步方法,例如 async_accept( ),那么您需要 run() io_service

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.

Smile简单爱 2024-12-22 05:34:23

你可能会发现它更容易阅读你的代码,

如果你输入 boost stuff example,

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

这不会直接帮助,但它会有所帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文