how to make boost asio async_accept to accept only one single connection?

1k views Asked by At

I'm using the exmple in http://www.boost.org/doc/libs/1_58_0/doc/html/boost_asio/examples/cpp11_examples.html HTTP server

How to change the example to accept only one single connection at once. That is just accept the next connection when the previous one has finished.

Thanks

2

There are 2 answers

1
Edgard Lima On

I did with:

            if (0 == connection_manager_.size()) {
            connection_manager_.start(std::make_shared<connection>(
                            std::move(socket_), connection_manager_, request_handler_));
            } else {
                std::move(socket_).close();
            }
1
sehe On

In server::do_accept simply do not include the last line (which is to start another async_accept).

void server::do_accept()
{
  acceptor_.async_accept(socket_,
      [this](boost::system::error_code ec)
      {
        // Check whether the server was stopped by a signal before this
        // completion handler had a chance to run.
        if (!acceptor_.is_open())
        {
          return;
        }

        if (!ec)
        {
          connection_manager_.start(std::make_shared<connection>(
              std::move(socket_), connection_manager_, request_handler_));
        }

        // do_accept(); // REMOVE THIS LINE
      });
}

As you can see this already used to stop accepting connections on close.