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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:55:32+00:00 2026-05-22T22:55:32+00:00

bool Connection::Receive(){ std::vector<uint8_t> buf(1000); boost::asio::async_read(socket_,boost::asio::buffer(buf,1000), boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); int rcvlen=buf.size(); ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen); if(rcvlen

  • 0
bool Connection::Receive(){
    std::vector<uint8_t> buf(1000);
    boost::asio::async_read(socket_,boost::asio::buffer(buf,1000),
            boost::bind(&Connection::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));

    int rcvlen=buf.size();
    ByteBuffer b((std::shared_ptr<uint8_t>)buf.data(),rcvlen);
    if(rcvlen <= 0){
        buf.clear();
        return false;
    }
    OnReceived(b);
    buf.clear();
    return true;
}

The method works fine but only when I make a breakpoint inside it. Is there an issue with timing as it waits to receive? Without the breakpoint, nothing is received.

  • 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-22T22:55:33+00:00Added an answer on May 22, 2026 at 10:55 pm

    You are trying to read from the receive buffer immediately after starting the asynchronous operation, without waiting for it to complete, that is why it works when you set a breakpoint.

    The code after your async_read belongs into Connection::handler, since that is the callback you told async_read to invoke after receiving some data.

    What you usually want is a start_read and a handle_read_some function:

    void connection::start_read()
    {
        socket_->async_read_some(boost::asio::buffer(read_buffer_),
            boost::bind(&connection::handle_read_some, shared_from_this(),
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));
    }
    
    void connection::handle_read_some(const boost::system::error_code& error, size_t bytes_transferred)
    {
        if (!error)
        {
            // Use the data here!
    
            start_read();
        }
    }
    

    Note the shared_from_this, it’s important if you want the lifetime of your connection to be automatically taken care of by the number of outstanding I/O requests. Make sure to derive your class from boost::enable_shared_from_this<connection> and to only create it with make_shared<connection>.

    To enforce this, your constructor should be private and you can add a friend declaration (C++0x version; if your compiler does not support this, you will have to insert the correct number of arguments yourself):

    template<typename T, typename... Arg> friend boost::shared_ptr<T> boost::make_shared(const Arg&...);
    

    Also make sure your receive buffer is still alive by the time the callback is invoked, preferably by using a statically sized buffer member variable of your connection class.

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

Sidebar

Related Questions

I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
Sometimes boost::asio seems to disconnect before I want it to, i.e. before the server
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
Is accessing a bool field atomic in C#? In particular, do I need to
Out style: bool result; if(something.TryParse(val, out result)) { DoSomething(result); } Nullable style: bool? result
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class Test { bool isVal() const { return isVal; } private: bool isVal; };
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
What is the difference between bool and Boolean types in C#?
Given the following: #light //any function returning bool * 'a let foo = let

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.