二级C++技巧:boost的网络库asio

发布时间:2011-08-29 共1页

  boost在1.35版本之后终于加入了网络库asio。春节期间终于得闲能够一窥究竟,同时将boost.asio和知名的ACE框架做一下比较。
  asio的名字突出了异步I/O的能力,从asio的文档中看到它使用了和ACE Proactor框架中相似的Proactor模式。提示CSDN上也有很多网友也写了很多关于异步I/O的好文章,但是还是决定从同步I/O开始。尽管阻塞I/O不是那么酷那么绚丽但是在网络编程中它和异步I/O一样重要。
  下面是一个简单的同步I/O的例子,使用的是锁步(lock-step)方式的通讯。
  view plaincopy to clipboardprint?
  #include <string>
  #include "boost/asio.hpp"
  #include "boost/lexical_cast.hpp"
  using namespace std;
  using namespace boost;
  using boost::asio::ip::tcp;
  using boost::asio::io_service;
  class Client
  {
  public:
  Client (const string & hostname, unsigned short port);
  virtual ~Client ();
  // methods
  virtual void send (const string & message);
  virtual string recv ();
  virtual void close ();
  private:
  io_service * io_service_;
  tcp::socket * socket_;
  };
  Client::Client (const string & hostname, unsigned short port)
  {
  io_service_ = new io_service();
  socket_ = new tcp::socket(*io_service_);
  tcp::resolver resolver(*io_service_);
  tcp::resolver::query query(hostname, boost::lexical_cast<string, unsigned short>(port));
  boost::system::error_code ec;
  tcp::resolver::iterator iter = resolver.resolve(query, ec);
  tcp::resolver::iterator end;
  // pick the first endpoint
  if (iter != end && ec == 0)
  {
  tcp::endpoint endpoint = *iter;
  std::cout << "Connecting to: " << endpoint << std::endl;
  socket_->connect(endpoint, ec);
  if (ec)
  {
  std::cerr << "Error: " << ec << std::endl;
  throw ec;
  }
  }
  }
  Client::~Client ()
  {
  delete socket_;
  delete io_service_;
  }
  void Client::send (const string & message)
  {
  boost::asio::const_buffers_1 request(message.data(), message.size());
  socket_->send(request);
  }
  string Client::recv ()
  {
  char response[128];
  size_t num = socket_->receive(boost::asio::buffer(response));
  if (num > 0)
  {
  return string (response, num);
  }
  return "";
  }
  void Client::close ()
  {
  socket_->close();
  }
  int _tmain(int argc, _TCHAR* argv[])
  {
  Client client ("localhost", 2009);
  std::cout << client.recv() << endl;
  string request;
  do
  {
  std::cout << "Request: ";
  std::cin >> request;
  if (request == "q")
  break;
  client.send (request);
  std::cout << "Response: " << client.recv() << endl;
  }
  while (true);
  client.close();
  return 0;
  }

百分百考试网 考试宝典

立即免费试用