boost::asio异步模式的C/S客户端源码实现

异步模式的服务器源码

//g++ -g async_tcp_server.cpp -o async_tcp_server -lboost_system
//

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/system/error_code.hpp>

using namespace std;
using namespace boost::asio;

class server{
    private:
        io_service &ios;
        ip::tcp::acceptor acceptor;
        typedef boost::shared_ptr<ip::tcp::socket> sock_ptr;
    public:
        server(io_service& io): ios(io),
            acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688)){ start(); }

        void start(){
            sock_ptr sock(new ip::tcp::socket(ios));
            acceptor.async_accept(*sock, bind(&server::accept_handler, this, placeholders::error, sock));
        }

        void accept_handler(const boost::system::error_code& ec, sock_ptr sock){
            if(ec) return;
            cout << "client: ";
            cout << sock->remote_endpoint().address() << endl;
            sock->async_write_some(buffer("hello asio"), bind(&server::write_handler, this, placeholders::error));
            start(); //retry to accept the next quest ......
        }

        void write_handler(const boost::system::error_code&){
            cout << "send msg complete." << endl;
        }
};

int main(){
    try{
        cout << "async tcp server start ......" << endl;
        io_service ios;

        server serv(ios);
        ios.run();
    }
    catch(std::exception& e){
        cout << e.what() << endl;
    }
}

异步模式的客户端源码

//g++ -g async_tcp_client.cpp -o async_tcp_client -lboost_system -lboost_date_time
//

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/shared_ptr.hpp>

using namespace std;
using namespace boost::asio;

class client{
    private:
        io_service& ios;
        ip::tcp::endpoint ep;
        typedef boost::shared_ptr<ip::tcp::socket> sock_ptr;
    public:
        client(io_service& io): ios(io),
            ep(ip::address::from_string("127.0.0.1"), 6688){start();}

        void start(){
            sock_ptr sock(new ip::tcp::socket(ios));
            sock->async_connect(ep, bind(&client::conn_handler, this, placeholders::error, sock));
        }

        void conn_handler(const boost::system::error_code& ec, sock_ptr sock){
            if(ec)  return;
            cout << "receive from " << sock->remote_endpoint().address() << endl;
            boost::shared_ptr<vector<char> > str(new vector<char>(100, 0));

            sock->async_read_some(buffer(*str), bind(&client::read_handler, this, placeholders::error, str));

            //sync timer block to control the client connection frequency
            deadline_timer t(ios, boost::posix_time::seconds(5));
            t.wait();
            start(); //retry to connect in the next time
        }

        void read_handler(const boost::system::error_code& ec, boost::shared_ptr<vector<char> > str){
            if(ec) return;
            cout << &(*str)[0] << endl;
        }
};

int main()
{
    try{
        cout << "client start ......" << endl;
        io_service ios;
        client cl(ios);
        ios.run();

        return 0;
    }
    catch(exception& e){
        cout << e.what() << endl;
    }
}

运行效果截图



参考文献

[1].罗剑锋, Boost程序库完全开发指南---深入C++"准"标准库

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值