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 8087343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:44:43+00:00 2026-06-05T18:44:43+00:00

this is my boost::asio server class Server: public boost::enable_shared_from_this<Server>, private boost::noncopyable{ private: boost::asio::ip::tcp::acceptor _acceptor;

  • 0

this is my boost::asio server

class Server: public boost::enable_shared_from_this<Server>, private boost::noncopyable{
  private:
    boost::asio::ip::tcp::acceptor _acceptor;
    boost::asio::ip::tcp::socket   _socket;
  public:
    explicit Server(boost::asio::io_service& ios, boost::asio::ip::tcp::endpoint& endpoint):_acceptor(ios, endpoint), _socket(ios){

    }
    void start(){
       accept();
    }
    void accept(){
       std::cout << "accepting " << std::endl;;
      _acceptor.async_accept(_socket, boost::bind(&Server::handler, this, boost::asio::placeholders::error));
    }
    void handler(const boost::system::error_code &ec){
       const std::string message = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
       if(!ec){
         boost::asio::async_write(_socket, boost::asio::buffer(message), boost::bind(&Server::write_handler, this));
       }else{
         std::cout << ec << std::endl;
       }
       accept();
    }
    void write_handler(){

    }
    boost::asio::ip::tcp::socket& socket(){
      return _socket;
    }
};


int main(){
  boost::asio::io_service ios;
  const unsigned int port = 5050;
  boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);

  Server server(ios, endpoint);
  server.start();

  ios.run();
  return 0;
}

for the first time it responds with an ‘Hallo World’;
Then it just keeps looping in accept <–> handler loop and doesn’t write the welcome message. ec prints

asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
......

and never stops

  • 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-06-05T18:44:46+00:00Added an answer on June 5, 2026 at 6:44 pm

    The infinite loop is the result of _socket being in use. The first async_accept() works because _socket is not in use. However, the _socket is never closed, so additional calls to async_accept() with _socket are going to fail. The async_accept()‘s peer argument expects that the socket is not in use, as it is going to use the socket for a new connection. This can be resolved by either:

    • Allocating a new socket per connection. Consider managing the sockets via boost::shared_ptr. This allows the server to handle multiple concurrent connections.
    • Closing the _socket in the write_handler, then invoke accept(). This limits the server to one connection at a time.

    Also, be careful with async_write(). Ownership of the underlying buffer memory is retained by the caller, who must guarantee that it remains valid until the handler is called. In this case, message will pop off of the stack before write_handler() is invoked. With message being const, consider making it static to guarantee its duration.

    Use shared_from_this() instead of this when passing in the object to the instance to the bind call. Otherwise, the instance pointed to by this may be deleted, as referencing counting only properly takes place when using shared_from_this().

    Finally, when printing boost::system::error_code, use the error_code.message() method to get a more meaningful message. In the case of the infinite loop, it would print “Already open”.

    Here is the modified handler() and write_handler() code that supports one connection at a time:

    void accept(){
       std::cout << "accepting " << std::endl;;
      _acceptor.async_accept(_socket, boost::bind(&Server::handler, shared_from_this(), boost::asio::placeholders::error));
    }
    void handler(const boost::system::error_code &ec){
       // Guarantee message will remain valid throughout the duration of async_write.
       static const std::string message = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
       if(!ec){
         // write_handler will accept the next connection once it is done with the socket.
         boost::asio::async_write(_socket, boost::asio::buffer(message), boost::bind(&Server::write_handler, shared_from_this()));
       }else{
         std::cout << ec.message() << std::endl;
         // Try accepting on error.
         accept();
       }
    }
    void write_handler(){
       _socket.close();
       // Now that the socket is closed, new connectiosn can be accepted.
       accept();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using boost::asio, and I have code like this: void CServer::Start(int port) { tcp::acceptor
#include <cstdlib> #include <iostream> #include <boost/bind.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; class session { public:
Alright, this is my current code snippet: namespace bai = boost::asio::ip; bai::tcp::socket tcp_connect(std::string hostname,
boost::asio::ip::tcp::socket& socket() const is returning _socket which is of type boost::asio::ip::tcp::socket is giving me
What is the proper way to shutdown an asynchronous boost asio tcp server? My
I am trying to Create a simple Tcp Server in C++ using Boost ASio
I'm writing a performance-critical bidirectional streaming server using boost.asio. The server works this way
I am receiving data from a server through a socket using boost asio, and
Quite new to boost and asio, need help: connect to proxy asio::ip::tcp::socket socket_; send
I have a class, which has a boost::asio::io_service object. I want this object stored

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.