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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:40:43+00:00 2026-06-17T07:40:43+00:00

I have a server application that uses boost::asio’s asynchronous read/write functions to communicate with

  • 0

I have a server application that uses boost::asio’s asynchronous read/write functions to communicate with connecting clients (until they disconnect on their side).

So far everything is good and fine but I would like to implement some kind of timed method where the server sends a packet by himself after a certain amount of time has passed.

I mainly followed the tutorials/examples on the boost::asio website so my program basically has the same structure as the given examples.

I tried to implement this feature by creating an asio::deadline timer object and passing it to the io_service object that I already “invoked” by calling io_service.run() like this:

asio::deadline_timer t(*io, posix_time::seconds(200));
t.async_wait(boost::bind(&connection::handle_timed, 
                this, boost::asio::placeholders::error));

And the handle_timed handler looks like this:

void connection::handle_timed(const system::error_code& error)
{
    //Ping packet is created here and gets stored in send_data

    async_write(socket_, asio::buffer(send_data, send_length), 
                boost::bind(&connection::handle_write, this, boost::asio::placeholders::error));
}

However the problem I have is that the deadline_timer doesn’t wait the given time, he almost immediately goes into the handler function and wants to send the packet.

It’s like he processes that asynchronous operations as soon as he gets to it and thats certainly not what I want to have.

Could it be that I can’t add new “objects” to an io_service object after it was being invoked with io_service.run()? Or maybe I have to specifically include it in the work queue of the io_service object afterwards?

Also I’m having trouble understanding how I can implement this without getting mixed up with the normal message traffic that I have.

  • 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-17T07:40:45+00:00Added an answer on June 17, 2026 at 7:40 am

    You can add work to an io_service at any time. You should check for errors in your async_wait() callback, it looks to me like your deadline_timer goes out of scope

    asio::deadline_timer t(*io, posix_time::seconds(200));
    t.async_wait(boost::bind(&connection::handle_timed, 
                    this, boost::asio::placeholders::error));
    ...
    // t goes out of scope here
    

    You should make it a member of your connection class just like socket_. Alternatively, use boost::enable_shared_from_this and keep a copy in your completion handlers:

    const boost::shared_ptr<asio::deadline_timer> t(new asio::deadline_timer(*io, posix_time::seconds(200)));
    t.async_wait(boost::bind(&connection::handle_timed, 
                    this, boost::asio::placeholders, t));
    

    and your completion handler

    void connection::handle_timed(
        const system::error_code& error,
        const boost::shared_ptr<asio::deadline_timer>& timer
        )
    {
        //Ping packet is created here and gets stored in send_data
    
        async_write(socket_, asio::buffer(send_data, send_length), 
                    boost::bind(&connection::handle_write, this, boost::asio::placeholders::error));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that uses rest to communicate to a server, i would
I have a Win32 application that uses boost::asio and openssl library but it seems
I have a client-server application that uses .net remoting. The clients are in a
I have to write simple client-server application that uses Unix datagram socket. Client may
We have a WinForms application that uses SQL server to store its data. To
I have developed a C# 4.0 desktop application that uses a SQL Server Express
I have a desktop application (C# .NET 3.5) that uses a SQL server for
I have an ASP.NET 3.0 application that works fine on one server. It uses
I have a server application that uses a lot of threads. Without wanting to
Background: We have a client/server application that uses a persistent connection to the server.

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.