zoukankan      html  css  js  c++  java
  • c++ boost asio库初学习

    前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考。

    同步server:

     1 // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
     2 //
     3 
     4 #include "stdafx.h"
     5 #include "boost/asio.hpp"
     6 #include "boost/bind.hpp"
     7 
     8 using namespace boost::asio;
     9 
    10 io_service service;
    11 size_t read_complete(char * buff, const boost::system::error_code & err, size_t bytes) {
    12     if (err) return 0;
    13     bool found = std::find(buff, buff + bytes, '
    ') < buff + bytes;
    14     // 我们一个一个读取直到读到回车,不缓存
    15     return found ? 0 : 1;
    16 }
    17 void handle_connections() {
    18     ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), 8001));
    19     char buff[1024];
    20     while (true) {
    21         ip::tcp::socket sock(service);
    22         acceptor.accept(sock);
    23         int bytes = read(sock, buffer(buff), boost::bind(read_complete, buff, _1, _2));
    24         std::string msg(buff, bytes);
    25         sock.write_some(buffer(msg));
    26         sock.close();
    27     }
    28 }
    29 int _tmain(int argc, char* argv[]) {
    30     handle_connections();
    31 }

    同步client:

     1 // boost_aiso_test.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
     2 //
     3 
     4 #include "stdafx.h"
     5 
     6 #include <iostream>
     7 #include <boost/asio.hpp>
     8 #include <boost/bind.hpp>
     9 #include <boost/shared_ptr.hpp>
    10 #include <boost/enable_shared_from_this.hpp>
    11 #include <boost/thread.hpp>
    12 
    13 using namespace boost::asio;
    14 
    15 
    16 io_service service;
    17 ip::address_v4 add;
    18 ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 8001);
    19 
    20 size_t read_complete(char * buf, const boost::system::error_code & err, size_t bytes)
    21 {
    22     if (err) return 0;
    23     bool found = std::find(buf, buf + bytes, '
    ') < buf + bytes;
    24     // 我们一个一个读取直到读到回车,不缓存
    25     return found ? 0 : 1;
    26 }
    27 void sync_echo(std::string msg) {
    28     msg += "
    ";
    29     ip::tcp::socket sock(service);
    30     sock.connect(ep);
    31     sock.write_some(buffer(msg));
    32     char buf[1024];
    33     int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));
    34     std::string copy(buf, bytes - 1);
    35     msg = msg.substr(0, msg.size() - 1);
    36     std::cout << "server echoed our " << msg << ": " << (copy == msg ? "OK" : "FAIL") << std::endl;
    37     sock.close();
    38 }
    39 int _tmain(int argc, char* argv[]) {
    40     char* messages[] = { "John says hi", "so does James", "Lucy just got home", "Boost.Asio is Fun!", 0 };
    41     boost::thread_group threads;
    42     for (char ** message = messages; *message; ++message) {
    43         threads.create_thread(boost::bind(sync_echo, *message));
    44         boost::this_thread::sleep(boost::posix_time::millisec(100));
    45     }
    46     threads.join_all();
    47 }

    -------------------------------------------------------- 异步是参考其他博客 --------------------------------------------------------

    异步server:

     1 #include "stdafx.h"
     2 
     3 #include <boost/asio.hpp>
     4 #include <iostream>
     5 using namespace boost::asio;
     6 class Server
     7 {
     8     typedef ip::tcp::socket socket_type;
     9     typedef std::shared_ptr<socket_type> sock_ptr;
    10 public:
    11     Server() :m_acceptor(m_io, ip::tcp::endpoint(ip::tcp::v4(), 6688))
    12     {
    13         accept();
    14     }
    15     void run()
    16     {
    17         m_io.run();
    18     }
    19 private:
    20     void accept()
    21     {
    22         sock_ptr sock(new socket_type(m_io));
    23         m_acceptor.async_accept(*sock,
    24             std::bind(&Server::accept_handler, this, std::placeholders::_1, sock));
    25     }
    26     void accept_handler(const boost::system::error_code& ec, sock_ptr sock)
    27     {
    28         if (ec)
    29         {
    30             return;
    31         }
    32         std::cout << "client:";
    33         std::cout << sock->remote_endpoint().address() << std::endl;
    34         sock->async_write_some(buffer("hello asio"),
    35             std::bind(&Server::write_handler, this, std::placeholders::_1));
    36         accept();
    37     }
    38     void write_handler(const boost::system::error_code&)
    39     {
    40         std::cout << "send msg complete." << std::endl;
    41     }
    42 private:
    43     io_service m_io;
    44     ip::tcp::acceptor m_acceptor;
    45 };
    46 int main()
    47 {
    48     try
    49     {
    50         std::cout << "server" << std::endl;
    51         Server svr;
    52         svr.run();
    53     }
    54     catch (std::exception& e)
    55     {
    56         std::cout << e.what() << std::endl;
    57     }
    58     return 0;
    59 }

    异步client:

     1 #include "stdafx.h"
     2 
     3 #include <boost/asio.hpp>
     4 #include <iostream>
     5 using namespace boost::asio;
     6 class Client
     7 {
     8     typedef ip::tcp::socket socket_type;
     9     typedef std::shared_ptr<socket_type> sock_ptr;
    10 public:
    11     Client() :m_ep(ip::address::from_string("127.0.0.1"), 6688)
    12     {
    13         start();
    14     }
    15     void run()
    16     {
    17         m_io.run();
    18     }
    19 private:
    20     void start()
    21     {
    22         sock_ptr sock(new socket_type(m_io));
    23         sock->async_connect(m_ep, std::bind(&Client::connect_handler, this, std::placeholders::_1, sock));
    24     }
    25     void connect_handler(const boost::system::error_code& ec, sock_ptr sock)
    26     {
    27         if (ec)
    28         {
    29             return;
    30         }
    31         std::cout << "receive from:" << sock->remote_endpoint().address() << std::endl;
    32         sock->async_read_some(buffer(m_buf),
    33             std::bind(&Client::read_handler, this, std::placeholders::_1));
    34     }
    35     void read_handler(const boost::system::error_code& ec)
    36     {
    37         if (ec)
    38         {
    39             return;
    40         }
    41         std::cout << m_buf << std::endl;
    42     }
    43 private:
    44     io_service m_io;
    45     ip::tcp::endpoint m_ep;
    46     enum { max_length = 1024 };
    47     char m_buf[max_length];
    48 };
    49 int main()
    50 {
    51     try
    52     {
    53         std::cout << "client start." << std::endl;
    54         Client cl;
    55         cl.run();
    56     }
    57     catch (std::exception& e)
    58     {
    59         std::cout << e.what() << std::endl;
    60     }
    61     getchar();
    62     return 0;
    63 }
  • 相关阅读:
    深入了解SQLServer系统数据库工作原理(转)
    什么是动态语言(转)
    ASP.NET 2.0客户端回调的实现分析
    什么是“分布式应用系统”
    SQLServer数据库安全管理机制详解
    什么是 CLR(转)
    docker容器下的asp.net core项目发布运维
    VLAN技术
    用getDrawingCache方法获取ImageView中的图像需要注意的问题
    交换机的工作原理
  • 原文地址:https://www.cnblogs.com/rarator/p/6183509.html
Copyright © 2011-2022 走看看