Boost asio 同步和异步socke处理的流程

时间:2022-09-08 22:12:09

一、同步处理socket

1、服务端

int main()
{
    try
    {
        std::cout << "Serveer start." << std::endl;
        io_service ios;
        ip::tcp::acceptor acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688));   //创建acceptor对象,接受6688端口
        std::cout << acceptor.local_endpoint().address << std::endl;

        while (true)                                                //服务一直执行
        {
            ip::tcp::socket sock(ios);     //创建一个socket对象,
            acceptor.accept(sock);          //阻塞等待socket对象连接
            std::cout << "client:";
            std::cout << sock.remote_endpoint().address() << std::endl;

            sock.write_some(buffer("hello asio."));    //发送数据   必须要用buffer包装
        }
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}

2、客户端

void client(io_service& ios)
{
    try
    {
        std::cout << "Client start..." << std::endl;
        //创建socket对象
        ip::tcp::socket sock(ios);
        //创建连接端点
        ip::tcp::endpoint ep(ip:; address::from_string("127.0.0.1"), 6688);
        //链接到端点
        sock.connect(ep);

        //定义缓冲区
        vector<char> vecStr(100, 0);
        //包装缓冲区数据, 并接受
        sock.read_some(buffer(vecStr));
        std::cout << "Client recive data from" << sock.remote_endpoint().address << std::endl;
        std::cout << &vecStr[0] << std::endl;
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}
main函数中可以用一个timer来实现

eg:

void main()
{
    io_service ios;
    //绑定函数
    a_timer at(ios, 5, bind(client, ref(ios)));
    ios.run();
}
二、异步处理

1、服务端

//异步服务
class server
{
public:
    server(io_service& io)
        : ios(io)
        , acceptor(ios, ip::tcp:; endpoints(ip::tcp::v4(), 6688)
    {
        start();
    }

    ~server();

    void start()
    {
        sock_pt sock(new ip::tcp::socket(ios));   //智能指针管理sock
                                                  //当tcp连接发生时,server::accept_handler()将被调用,使用sock对象发送数据
        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::endl;
        std::cout << sock->remote_endpoint().address() << std::endl;
        sock->async_write_some(buffer("Hello world"), bind(&server::write_handler, this, placeholders::error));   //异步发送数据
        start();
    }

    void write_handler(const system::error_code& ec)
    {
        std::cout << "write_handler" << std::endl;
        return;
    }

private:
    io_service ios;
    ip::tcp::acceptor acceptor;
    using shared_ptr<ip::tcp::socket> = sock_pt;
};

int main()
{
    try
    {
        io_service ios;
        server server(ios);
        ios.run();
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}
2、客户端
//客户端
class client
{
public:
    client(io_service& io)
        : ios(io)
        , eq(ip::address::from_string("127.0.0.1"), 6688)
    {
        start();
    }
    void start()
    {
        sock_pt sock(new ip::tcp:;socket(ios));
        sock->async_connect(eq, bind(&client::conn_handler, this, placeholders::error, sock));
    }

    void conn_handler(const system::error_code& ec, sock_pt sock)
    {
        if (ec)
        {
            return;
        }
        std::cout << "revice from" << sock->remote_endpoint().address();
        shared_ptr<vector<char>> vecStr(new vector<char>(100, 0));

        sock->async_read_some(buffer(*vecStr), bind(&client::read_handler, this, placeholders::error, vecStr));
        start();
    }
    //当异步连接成功时,conn_handler将会被调用,它在调用shared_ptr包装的vector作为数据缓冲区,由async_read_some异步读取,然后在启用一个异步连接
    void read_handler(const system::error_code& ec, shared_ptr<vector<char>> vecStr)
    {
        if (ec)
        {
            return;
        }
        std::cout << &(*vecStr)[0] << std::endl;
    }

    ~client();

private:
    io_service ios;
    ip::tcp::endpoint eq;
    using shared_ptr<ip::tcp::socket> = sock_pt;
};

int main()
{
    try
    {
        std::cout << "client start.";
        io_service ios;
        client cc(ios);
        ios.run();
    }
    catch (const std::exception& e)
    {
        std::cout << e.what() << std::endl;
    }
}