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

  • Home
  • SEARCH
  • 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 8609759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:57:32+00:00 2026-06-12T03:57:32+00:00

I’m trying to make a toy asynchronous echo client using the boost asio. For

  • 0

I’m trying to make a toy asynchronous echo client using the boost asio. For some reason it gets blocked on the second request/reply cycle while waiting for sent_=true, but before receiving the echo (w/o the server crashing).

/*
UDP asynchronous clint using boost asio library
 */
#include <cstdlib>
#include <iostream>
#include <algorithm>

#include <boost/array.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/chrono/duration.hpp>

#include "dbook/test/tools.hpp"

using boost::asio::ip::udp;

const size_t N_MESSAGES = 1024;

class udp_client {
public:
  udp_client(const std::string& host,const std::string& service)
    :io_service_(),
     socket_(io_service_),
     replied_(false),
     sent_(false)
  {
    socket_.open(boost::asio::ip::udp::v4());
    udp::resolver resolver(io_service_);
    udp::resolver::query query(udp::v4(), host,  service);
    endpoint_ = *resolver.resolve(query);

  }
  ~udp_client() {
    socket_.close();
  }

  bool sent() {
    return sent_;
  }

  void send(const int r) {
    replied_ = false;
    sent_ = false;

    memcpy(&send_buf_[0],&r,sizeof(int));
    std::cout << "prepare sending" << std::endl;

    socket_.async_send_to(boost::asio::buffer(send_buf_), endpoint_,
              boost::bind(&udp_client::handle_send, this,
                      boost::asio::placeholders::error,
                      boost::asio::placeholders::bytes_transferred));
    io_service_.run_one();

    std::cout << "after run_one" << std::endl;

  }

  bool replied() {
    return replied_;
  }

  int reply() {
    return *reinterpret_cast<int*>(&recv_buf_[0]);
  }

private:
  void handle_send(const boost::system::error_code& error,
           std::size_t size)
  {
    if (error) {
      //missing error propagation to main thread
      std::cerr << "ERROR: Client error while sending (error code = " << error << "): "  << std::endl;
      std::cerr << "ERROR: Recovering..." << std::endl;

    } else {
      sent_ = true;
      std::cout << "sent" << std::endl;
      socket_.async_receive_from(boost::asio::buffer(recv_buf_), endpoint_,
                 boost::bind(&udp_client::handle_receive, this,
                         boost::asio::placeholders::error,
                         boost::asio::placeholders::bytes_transferred));
      io_service_.run_one();


    }
  }

  void handle_receive(const boost::system::error_code& error,
              std::size_t size) {
    if (error) {
      //missing error propagation to main thread
      std::cerr << "ERROR: Client error while receiving (error code = " << error << ")" << std::endl;
      std::cerr << "ERROR: Recovering..." << std::endl;

    } else {
      std::cout << "received" << std::endl;

      replied_ = true;
    }
  }


private:
  boost::asio::io_service io_service_;
  udp::socket socket_;
  udp::endpoint endpoint_;
  volatile bool replied_;
  volatile bool sent_;
  volatile int reply_;
  boost::array<char, sizeof(int)> send_buf_;
  boost::array<char, sizeof(int)> recv_buf_;

};

int main(int argc, char* argv[]) {

  if (argc != 3) {
    std::cerr << "Usage: udp_echo_client <host> <port>" << std::endl;
    return 1;
  }

  try {
    udp_client c(argv[1],argv[2]);

    for(size_t i=0; i != N_MESSAGES; ++i) {
      int r = rand();
      c.send(r);

      //here we could put a tiemeout
      while (!c.sent()) {
    boost::this_thread::sleep(boost::posix_time::milliseconds(10)); 
      }

      //here we could put a tiemeout
      while (!c.replied()) {
    boost::this_thread::sleep(boost::posix_time::milliseconds(10)); 
      }
      int resp = c.reply();

      std::cout << "sent= " << r << ", received= " << resp << std::endl;
      assert(r == resp);
    }

  } catch (std::exception& e) {
    std::cerr << "ERROR: " << e.what() << std::endl;
  }

  return 0;
}

The log I get is:

$ ./bin/udp_echo_client localhost 11111
prepare sending
sent
received
after run_one
sent= 16807, received= 16807
prepare sending
after run_one

I guess it is my lack of understanding on how to use boost asio. So, I will appreciate if someone can explain why the program behaves like that:)

  • 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-12T03:57:33+00:00Added an answer on June 12, 2026 at 3:57 am

    Promoting my comment, as suggested:

    Generally, you need to be continuously polling the io_service (or more approrpaitely, just call io_service::run. In this particular case, you need to call io_service::reset() after io_service::run_one() has completed.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.