0
votes

I'm starting to use Boost, so may be I'm messing something up.

I'm trying to set up http server with boost (ASIO). I've taken the code from docs: http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/examples/cpp03_examples.html (HTTP Server, the first one)

The only difference from the example is I'm running server by my own method "run" and starting io_service in background thread, like in the docs: http://www.boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/io_service.html

boost::asio::io_service::work work(io_service_);

(Also I'm stopping io_service from my run method too.)

When I'm starting this modified server everything seems to be OK, run method is working fine. But then I'm trying to get a doc from the server the request hangs and control flow never comes to "request_handle" method. Am I missing something?

UPD. Here is my code of run method:

void NetstreamServer::run()
{
  LOG4CPLUS_DEBUG(logger, "NetstreamServer is running");
  boost::asio::io_service::work work(io_service_);
    try
    {
      while (true)
      {
        if (condition)
        {
          io_service_.stop();
          break;
        }
      }
    }
    catch (std::exception const& e)
    {
      LOG4CPLUS_ERROR(logger, "NetstreamServer" << " caught exception: " << e.what());
    }
}
2
Where is io_service_.run() ?Igor R.
May be I'm misunderstanding the doc (boost.org/doc/libs/1_54_0/doc/html/boost_asio/reference/…), but AFAIU since I'm creating "work" object I do not need this.Val
Work object only ensures that io_service::run won't exit (quoting from your link: "This ensures that the io_service object's run() function will not exit while work is underway, and that it does exit when there is no unfinished work remaining."). But if you don't call io_service::run, no one will do that for you.Igor R.
OK, thank you, now I get it.Val

2 Answers

1
votes

You should call io_service_::run() - otherwise no one will dispatch the completion handlers of Asio objects serviced by io_service_.

0
votes

Without including the code you changed, everyone here can only guess. Unfortunately you also do not include the compiler and the OS you are using. Even with boost claiming it is platform independent, you should always include this information, as it reality, platforms are different even with boost.

Let me do a guess. You use Microsoft Windows? How do you prevent the "main" function to exit? You moved the blocking "run" function out of it in another thread, the main function has no wait point anymore. Let me guess again, you used something like "getchar". With that, you can exit your server with only hitting the keyboard return key. If yes, the problem is the getchar, with unfortunately blocks every io of the asio socket implementation, but only on Windows based systems.

I would not need to guess if you would include the informations mentioned in your post. In particular all(!) changes you made to the code sample.