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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:25:03+00:00 2026-06-03T00:25:03+00:00

I am looking at the asio example in http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/example/timeouts/async_tcp_client.cpp Here’s what I am having

  • 0

I am looking at the asio example in http://www.boost.org/doc/libs/1_44_0/doc/html/boost_asio/example/timeouts/async_tcp_client.cpp

Here’s what I am having real trouble to understand:

  1. Why does handle_read call back start_read again?
  2. What happens when the timer expires? I see no callback routine provided to the timer.

void start_read()
{
// Set a deadline for the read operation.
deadline_.expires_from_now(boost::posix_time::seconds(30));

// Start an asynchronous operation to read a newline-delimited message.
boost::asio::async_read_until(socket_, input_buffer_, '\n',
    boost::bind(&client::handle_read, this, _1));   

}

void handle_read(const boost::system::error_code& ec)
{
if (stopped_)
return;

if (!ec)
{
  // Extract the newline-delimited message from the buffer.
  std::string line;
  std::istream is(&input_buffer_);
  std::getline(is, line);

  // Empty messages are heartbeats and so ignored.
  if (!line.empty())
  {
    std::cout << "Received: " << line << "\n";
  }

  start_read();
}
else
{
  std::cout << "Error on receive: " << ec.message() << "\n";

  stop();
}   

}

  • 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-06-03T00:25:04+00:00Added an answer on June 3, 2026 at 12:25 am

    Why does handle_read call back start_read again?

    If it didn’t, the client would only ever read the socket once and then never again. So on a successful read, the client wants to attempt a read again. That makes for perpetual reading of the socket.

    What happens when the timer expires? I see no callback routine provided to the timer.

    The code is towards the top of the source file:

    deadline_.async_wait(boost::bind(&client::check_deadline, this));
    

    The check_deadline() function will close the socket if the deadline has passed.

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

Sidebar

Related Questions

I'm having trouble implmenting the 3rd parameter in the function documented here: http://www.boost.org/doc/libs/1_39_0/doc/html/boost_asio/reference/async_read_until/overload4.html What
I'm looking for the best way to modify the Boost Asio HTTP Server 3
I am going through the boost::asio examples. I am looking at Example 4 What
I'm looking for something like Twisted in Python (event-driven networking engine). I've found Boost:Asio
Is I am looking at writing a multithreaded tcp server using boost ASIO. I
The sample I'm looking at in full is: #include <boost/asio.hpp> #include <boost/bind.hpp> #include <boost/enable_shared_from_this.hpp>
My application is a TCP client that closely resembles the Boost.Asio chat client example
I'm starting with boost asio programming in C++ and when looking over the examples
There is a function assign in Boost.Asio sockets, however I'm looking for something like
Looking at the example here - http://jsfiddle.net/uqYeQ/3/ The first row behaves as expected, returning

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.