Boost logo

Boost Users :

Subject: [Boost-users] asio: question regarding design
From: Goran (sendmailtogoran_at_[hidden])
Date: 2012-09-05 12:05:55


Hi all,

I wanna change the design of some async client (code at end of this
message) towards something like "run as a server and pull a message
from another server".

The so called "another server" will be my master server in future. At
the moment I'm taking some example from asio's documentation:
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/tutorial/tutdaytime3.html

The async client is derived from another example of asio's documentation:
http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/example/http/client/async_client.cpp

My simple question is: How would you change the code below to run in a
loop and pulls 'let's say' every second a message from master server?

Thanks a lot!

#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using boost::asio::ip::tcp;

class client
{
public:
  client(boost::asio::io_service& io_service,
      const std::string& server, const std::string& port)
    : resolver_(io_service),
      socket_(io_service)
  {
    // Start an asynchronous resolve to translate the server and service names
    // into a list of endpoints.
    tcp::resolver::query query(server, port);
    resolver_.async_resolve(query,
        boost::bind(&client::handle_resolve, this,
          boost::asio::placeholders::error,
          boost::asio::placeholders::iterator));
  }

private:
  void handle_resolve(const boost::system::error_code& err,
      tcp::resolver::iterator endpoint_iterator)
  {
    if (!err)
    {
      // Attempt a connection to each endpoint in the list until we
      // successfully establish a connection.
      tcp::endpoint endpoint = *endpoint_iterator;
      socket_.async_connect(endpoint,
          boost::bind(&client::handle_connect, this,
            boost::asio::placeholders::error));
    }
    else
    {
      std::cout << "Error: " << err.message() << "\n";
    }
  }

  void handle_connect(const boost::system::error_code& err)
  {
    if (!err)
    {
      // The connection was successful. Read the response.
      boost::asio::async_read(socket_, response_,
          boost::bind(&client::handle_response, this));
    }
    else if (err != boost::asio::error::eof)
    {
      std::cout << "Error: " << err.message() << "\n";
    }
  }

  void handle_response()
  {
    if (response_.size() > 0)
      std::cout << "Response: " << &response_;
    else
      std::cout << "Response is empty." << std::endl;
  }

  tcp::resolver resolver_;
  tcp::socket socket_;
  boost::asio::streambuf request_;
  boost::asio::streambuf response_;
};

int main(int argc, char* argv[])
{
  try
  {
    if (argc != 3)
    {
      std::cout << "Usage: async_client <server> <port>\n";
      std::cout << "Example:\n";
      std::cout << " async_client localhost 2020\n";
      return 1;
    }

    boost::asio::io_service io_service;
    client c(io_service, argv[1], argv[2]);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cout << "Exception: " << e.what() << "\n";
  }

  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net