Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:40:27+00:00 2026-05-26T21:40:27+00:00

I am trying to create a tcp synchronous server. My main thread would create

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:40:28+00:00Added an answer on May 26, 2026 at 9:40 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a TCP connection and send/read data that uses SSL, but
I am trying to create a ftp server in c#. This server should listen
I am trying to create a TCP connection from an embedded controller to a
I am trying to Create a simple Tcp Server in C++ using Boost ASio
I'm trying to create a TCP server where the Start() method blocks until a
i'm trying to find out how to create a TCP server with SSL in
Trying to create my first iPhone app that would play back audio. When I
I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7
I'm trying to create a client-server program that send and receive a string (its
I'm trying to create a use-once HTTP server to handle a single callback and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.