LinuxC/C++編程基礎(16) boost非同步socket處理

來源:互聯網
上載者:User

一.伺服器端server.cpp的實現,如下:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost;
using namespace boost::asio;
class server{
private:
    io_service& ios;
    ip::tcp::acceptor acceptor;
    typedef shared_ptr<ip::tcp::socket> sock_pt;
public:
    server(io_service& io):ios(io)
        ,acceptor(ios,ip::tcp::endpoint(ip::tcp::v4(),9999))
    {
        start();
    }
    void start(){
        sock_pt sock(new ip::tcp::socket(ios));
        acceptor.async_accept(*sock,bind(&server::accept_handler,this,placeholders::error,sock));
    }
    void accept_handler(const system::error_code& ec,sock_pt sock){
        if(ec){
            return;
        }
        std::cout<<"client: ";
        std::cout<<sock->remote_endpoint().address()<<std::endl;
        sock->async_write_some(buffer("hello asio"),bind(&server::write_handler,this,placeholders::error));
        start();
    }
    void write_handler(const system::error_code& e){
        std::cout<<"send message complete."<<std::endl;
    }
};
int main(int argc,char** argv){
    try{
        std::cout<<"server start."<<std::endl;
        io_service ios;
        server serv(ios);
        ios.run();
    }catch(std::exception& e){
        std::cout<<e.what()<<std::endl;
    }
    return 0;
}

二.用戶端client.cpp的實現,如下:

#include <iostream>
#include <vector>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost;
using namespace boost::asio;

class client{
private:
    io_service& ios;
    ip::tcp::endpoint ep;
    typedef shared_ptr<ip::tcp::socket> sock_pt;
public:
    client(io_service& io):ios(io)
        ,ep(ip::address::from_string("127.0.0.1"),9999)
    {
        start();
    }
    void start(){
        sock_pt sock(new ip::tcp::socket(ios));
        sock->async_connect(ep,bind(&client::conn_handler,this,placeholders::error,sock));
    }
    void conn_handler(const system::error_code& ec,sock_pt sock){
        if(ec){
            return;
        }
        std::cout<<"receive from "<<sock->remote_endpoint().address()<<std::endl;
        shared_ptr<std::vector<char> > str(new std::vector<char>(100,0));
        sock->async_read_some(buffer(*str),bind(&client::read_handler,this,placeholders::error,str));
        start();
    }
    void read_handler(const system::error_code& ec,shared_ptr<std::vector<char> >str){
        if(ec){
            return;
        }
        std::cout<<&(*str)[0]<<std::endl;
    }
};

int main(int argc,char** argv){
    try{
        std::cout<<"client start."<<std::endl;
        io_service ios;
        client cli(ios);
        ios.run();
    }catch(std::exception& e){
        std::cout<<e.what()<<std::endl;
    }
    return 0;
}

三.運行結果,如下:

伺服器端:


用戶端:



參考文獻:boost庫完全開發指南,羅劍鋒 著

轉載請註明出處:山水間部落格:http://blog.csdn.net/linyanwen99/article/details/8256266

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.