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

The Archive Base Latest Questions

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

I am trying to receive data from a server application using boost asio’s async_read()

  • 0

I am trying to receive data from a server application using boost asio’s async_read() free function, but the callback I set for when the receiving is never called.

The client code is like this:

Client::Client()
{
  m_oIoService.run(); // member boost::asio::io_service
  m_pSocket = new boost::asio::ip::tcp::socket(m_oIoService);

  // Connection to the server
  [...]

  // First read
  boost::asio::async_read(*m_pSocket,
                          boost::asio::buffer((void*)&m_oData, sizeof(m_oData)),
                          boost::bind(&Client::handleReceivedData, this,
                                      boost::asio::placeholders::error,
                                      boost::asio::placeholders::bytes_transferred));
}

I tried with small data (a short string) and I can’t get it to work. When I use the synchronous read function (boost::asio::read()) using the two same first parameters, everything works perfectly.

Am I missing something with the use of the io_service? I am still unsure about how it works.

  • 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-26T04:40:05+00:00Added an answer on May 26, 2026 at 4:40 am

    boost::asio::service::run () is a blocking call. Now, in your example it may or may not return immediately. In case it doesn’t, you you are blocked even before you create a socket, and never call read, so cannot expect a callback. Otherwise, dispatch loop is exited, so no callbacks are ever delivered.

    Read more about boost::asio::service::run (). I recommend you check out documentation including tutorial, examples and reference. It is worth going trough it in full to understand the concept.

    Hope it helps!

    P.S.: On a side note, your code is not exception safe. Beware that if constructor of the class fails with exception then destructor of that class instance is never called. Thus, you may leak at least m_pSocket if its type is not one of the “smart pointers”. You should consider making it exception safe, moving the code to another method that should be called by user, or even wrapping this functionality with a free function.

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

Sidebar

Related Questions

I am trying to receive a json object back from php after sending data
i'm trying to send/receive data over wifi to an iphone/ipodtouch app from a C#
I am trying to receive an image from the Android Gallery via an ACTION_SEND
I receive the following error when trying to run my ASP.NET MVC application: The
I'm trying to send and receive using pipes: send.cpp struct { long a; long
I'm trying perform a relatively basic TCP socket send/receive operation on Windows CE using
I'm trying to build a server that will receive files sent by clients over
i'm trying to create a bridge program in twisted.web that receives data from a
I'm trying to make a MFC application(client) that connects to a server on (localhost,port
I'm trying to build a Python application using pyGTK, treads, and sockets. I'm having

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.