Chinaunix首页 | 论坛 | 博客
  • 博客访问: 8701
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 36
  • 用 户 组: 普通用户
  • 注册时间: 2014-01-01 20:31
个人简介

一个苦比逼的程序员!

文章分类
文章存档

2014年(4)

我的朋友

分类: C/C++

2014-03-17 14:17:47

原文地址:boost asio 简单示例 作者:smzgl

客户端:
  1. #include <iostream>
  2. #include <boost/asio.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/shared_ptr.hpp>
  5. #include <boost/enable_shared_from_this.hpp>

  6. using boost::asio::ip::tcp;
  7. using boost::asio::ip::address;

  8. class session: public boost::enable_shared_from_this<session> {
  9. public:
  10.     session(boost::asio::io_service &io_service, tcp::endpoint &endpoint)
  11.         : io_service_(io_service), socket_(io_service), endpoint_(endpoint)
  12.     {
  13.     }

  14.     void start() {
  15.         socket_.async_connect(endpoint_,
  16.                 boost::bind(&session::handle_connect,
  17.                     shared_from_this(),
  18.                     boost::asio::placeholders::error));
  19.     }

  20. private:
  21.     void handle_connect(const boost::system::error_code &error) {
  22.         if (error) {
  23.             if (error.value() != boost::system::errc::operation_canceled) {
  24.                 std::cerr << boost::system::system_error(error).what() << std::endl;
  25.             }

  26.             socket_.close();
  27.             return;
  28.         }

  29.         static tcp::no_delay option(true);
  30.         socket_.set_option(option);

  31.         strcpy(buf, "Hello World!\n");
  32.         boost::asio::async_write(socket_,
  33.                 boost::asio::buffer(buf, strlen(buf)),
  34.                 boost::bind(&session::handle_write,
  35.                     shared_from_this(),
  36.                     boost::asio::placeholders::error,
  37.                     boost::asio::placeholders::bytes_transferred));
  38.     }

  39.     void handle_write(const boost::system::error_code& error, size_t bytes_transferred) {
  40.         memset(buf, sizeof(buf), 0);
  41.         boost::asio::async_read_until(socket_,
  42.                 sbuf,
  43.                 "\n",
  44.                 boost::bind(&session::handle_read,
  45.                     shared_from_this(),
  46.                     boost::asio::placeholders::error,
  47.                     boost::asio::placeholders::bytes_transferred));
  48.     }

  49.     void handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
  50.         std::cout << buf << std::endl;
  51.     }

  52. private:
  53.     boost::asio::io_service &io_service_;
  54.     tcp::socket socket_;
  55.     tcp::endpoint &endpoint_;
  56.     char buf[1024];
  57.     boost::asio::streambuf sbuf;
  58. };

  59. typedef boost::shared_ptr<session> session_ptr;

  60. int main(int argc, char* argv[])
  61. {
  62.     boost::asio::io_service io_service;
  63.     tcp::endpoint endpoint(address::from_string("192.168.1.1"), 10028);

  64.     session_ptr new_session(new session(io_service, endpoint));
  65.     new_session->start();
  66.     io_service.run();
  67.     return 0;
  68. }

服务器:
  1. #include <string.h>
  2. #include <boost/asio.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/shared_ptr.hpp>
  5. #include <boost/enable_shared_from_this.hpp>

  6. using boost::asio::ip::tcp;
  7. using boost::asio::ip::address;

  8. class session: public boost::enable_shared_from_this<session> {
  9. public:
  10.     session(boost::asio::io_service &io_service): socket_(io_service)
  11.     {
  12.     }

  13.     void start() {
  14.         static tcp::no_delay option(true);
  15.         socket_.set_option(option);

  16.         boost::asio::async_read_until(socket_,
  17.                 sbuf_,
  18.                 "\n",
  19.                 boost::bind(&session::handle_read,
  20.                     shared_from_this(),
  21.                     boost::asio::placeholders::error,
  22.                     boost::asio::placeholders::bytes_transferred));
  23.     }

  24.     tcp::socket &socket() {
  25.         return socket_;
  26.     }

  27. private:
  28.     void handle_write(const boost::system::error_code& error, size_t bytes_transferred) {
  29.         boost::asio::async_read_until(socket_,
  30.                 sbuf_,
  31.                 "\n",
  32.                 boost::bind(&session::handle_read,
  33.                     shared_from_this(),
  34.                     boost::asio::placeholders::error,
  35.                     boost::asio::placeholders::bytes_transferred));
  36.     }

  37.     void handle_read(const boost::system::error_code& error, size_t bytes_transferred) {
  38.         boost::asio::async_write(socket_,
  39.                 sbuf_,
  40.                 boost::bind(&session::handle_write,
  41.                     shared_from_this(),
  42.                     boost::asio::placeholders::error,
  43.                     boost::asio::placeholders::bytes_transferred));
  44.     }

  45. private:
  46.     tcp::socket socket_;
  47.     boost::asio::streambuf sbuf_;
  48. };

  49. typedef boost::shared_ptr<session> session_ptr;

  50. class server {
  51. public:
  52.         server(boost::asio::io_service &io_service, tcp::endpoint &endpoint)
  53.             : io_service_(io_service), acceptor_(io_service, endpoint)
  54.         {
  55.             session_ptr new_session(new session(io_service_));
  56.             acceptor_.async_accept(new_session->socket(),
  57.                 boost::bind(&server::handle_accept,
  58.                     this,
  59.                     new_session,
  60.                     boost::asio::placeholders::error));
  61.         }

  62.         void handle_accept(session_ptr new_session, const boost::system::error_code& error) {
  63.             if (error) {
  64.                 return;
  65.             }

  66.             new_session->start();
  67.             new_session.reset(new session(io_service_));
  68.             acceptor_.async_accept(new_session->socket(),
  69.                 boost::bind(&server::handle_accept,
  70.                     this,
  71.                     new_session,
  72.                     boost::asio::placeholders::error));
  73.         }

  74.         void run() {
  75.             io_service_.run();
  76.         }

  77. private:
  78.         boost::asio::io_service &io_service_;
  79.         tcp::acceptor acceptor_;
  80. };

  81. int main(int argc, char* argv[])
  82. {
  83.     boost::asio::io_service io_service;
  84.     tcp::endpoint endpoint(address::from_string("192.168.1.1"), 10028);

  85.     server s(io_service, endpoint);
  86.     s.run();
  87.     return 0;
  88. }

编译:
g++ -Wall -o client client.cpp -lboost_system
g++ -Wall -o server server.cpp -lboost_system

这里需要注意的是: async_write, async_read, async_read_until 都是需要达到特定条件才会调用回调函数,
在调用回调函数之前, 不能再次调用, 否则接收到的数据很可能是乱的. 所以, 在实际代码当中, 会有一个写缓冲队列, 当需要write的时, 先放到队列中, 如果队列个数为1, 则调用async_write, 否则等待函数回调, 当函数回调时将首个元素从队列中移除, 然后接着发送下一个, 直到队列为空.

对于client, 由于is_open在async_connect之后就是true状态了, 因此在async_connect回调返回之前没有方法知道是否已经连接成功, 实际代码当中一般会增加一个变量以表示该套接字是否已经允许发送数据.

阅读(756) | 评论(0) | 转发(0) |
0

上一篇:0123C++补习

下一篇:boost asio allocation

给主人留下些什么吧!~~