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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:58:35+00:00 2026-05-22T00:58:35+00:00

I am building an HTTP client based on the example on HTTP server given

  • 0

I am building an HTTP client based on the example on HTTP server given at boost website. Now, the difference between that code and mine is that the example uses the server constructor to start the asynchronous operations. This makes sense since a server is supposed to listen all the time. In my client, on the other hand, I want to first construct the object and then have a send() function that starts off by connecting to the endpoint and later on sends a request and finally listens for the reply. This makes sense too, doesn’t it?

When I create my object (client) I do it in the same manner as in the server example (winmain.cpp). It looks like this:

client c("www.boost.org);
c.start(); // starts the io_service in a thread
c.send(msg_);

The relevant parts of the code are these:

void enabler::send(common::geomessage& msg_)
{
    new_connection_.reset(new connection(io_service_,
        connection_manager_,
        message_manager_, msg_
    ));

    boost::asio::ip::tcp::resolver resolver(io_service_);
    boost::asio::ip::tcp::resolver::query query(host_address, "http");

    resolver.async_resolve(query, boost::bind(
        &enabler::handle_resolve,
        boost::ref(*this),
        boost::asio::placeholders::error,
        boost::asio::placeholders::iterator
    ));
}

void enabler::run()
{
    io_service_.run();
}

The problem with this is that the program gets stuck somewhere here. The last thing that prints is the “Resolving host”, after that the program ends. I don’t know why because the io_service should block until all async operations have returned to their callbacks. If, however, I change the order of how I call the functions, it works. If I call run() just after the call to async_resolve() and also omit calling start() in my main program, it works!

In this scenario, io_service blocks as it should and I can see that I get a response from the server.

It has something to do from the fact that I call run() from inside the same class as where I call async_resolve(). Could this be true? The I suppose I need to give a reference from the main program when I call run(), is it like that?

I have struggled with getting io_service::work to work but the program just gets stuck and yeah, similar problems as the one above occur. So it does not really help.

So, what can I do to get this right? As I said earlier, what I want is to be able to create the client object and have the io_service running all the time in a separate thread inside the client class. Secondly to have a function, send(), that sends requests to the server.

  • 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-22T00:58:35+00:00Added an answer on May 22, 2026 at 12:58 am

    If you don’t expect to have some work at all times, to keep the io_service busy, you should construct an io_service::work object in some scope which can be exited without io_service::run() having to return first. If you’re running the io_service in a separate thread, I would imagine you wouldn’t have a problem with that.

    It’s sort of hard to know what you’re trying to do with those snippets of code. I imagine that you’d want to do something along these lines:

    struct client
    {
       io_service io_service_;
       io_service::work* w_;
       pthread_t main_thread_;
       client(): w_(new io_service::work(io_service)) { ... }
       void start() { pthread_create(&main_thread_, 0, main_thread, this); }
       static long main_thread(void* arg) { ((client*)arg)->io_service_.run(); }
       // release the io_service and allow run() to return
       void stop() { delete w_; w_ = 0; pthread_join(main_thread_); }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an ogre3d tutorial based on this setup http://www.ogre3d.org/wiki/index.php/SettingUpAnApplication#XCode I've followed all steps
When building a multi-lingual website (with ASP.NET web forms), I'll use an HTTP module
I am building an AJAX application that uses both HTTP Content and HTTP Header
Building a client-side swing application what should be notified on a bus (application-wide message
Folks, I'm building a pretty standard workflow that I want exposed via a WCF
The scenario: I’m building a website, which will not Host the images in its
I'm developing an application for a client's website. Here's the story, this company uses
I'm building something similar to this - http://www.impressivewebs.com/demo-files/content-switcher/content-switcher.html I wondered if anyone had any
Building on How Do You Express Binary Literals in Python , I was thinking
Building the same project (without any changes) produces binary different exe-files: some small regions

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.