• 使用 boost.asio 简单实现 异步Socket 通信


     客户端:

    class IPCClient {
    public:
        IPCClient();
        ~IPCClient();
        bool run();
    private:
        bool connect();
        bool conn_handler(const boost::system::error_code&ec, boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
        bool read_handler(const boost::system::error_code&ec, boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
    private:
        boost::asio::io_service m_io;
        std::vector<char> m_buf;
        boost::asio::ip::tcp::endpoint m_ep;
    };
    #include "IPCClient.h"
    using namespace std;
    using namespace boost::asio;
    typedef ip::tcp::acceptor acceptor_type;
    typedef ip::tcp::endpoint endpoint_type;
    typedef ip::tcp::socket socket_type;
    typedef ip::address address_type;
    typedef boost::shared_ptr<socket_type> sock_ptr;
    typedef vector<char> buffer_type;
    
    IPCClient::IPCClient():m_buf(100, 0), m_ep(address_type::from_string("127.0.0.1"), 6666) {
        connect();
    }
    
    IPCClient::~IPCClient() {
        cout << "客户端退出" << endl;
    }
    
    bool IPCClient::connect() {
        sock_ptr sock(new socket_type(m_io));
        sock->async_connect(m_ep, boost::bind(&IPCClient::conn_handler, this, boost::asio::placeholders::error, sock));
        return true;
    }
    
    bool IPCClient::run() {
        m_io.run();
        return true;
    }
    
    bool IPCClient::conn_handler(const boost::system::error_code &ec, boost::shared_ptr<boost::asio::ip::tcp::socket> sock) {
        if (ec) {
            cout << "异步连接错误!请检查配置" << endl;
            return false;
        }
        cout<<"服务端信息:"<<sock->remote_endpoint().address()<< ":" << sock->remote_endpoint().port() <<endl;
        sock->async_read_some(buffer(m_buf), boost::bind(&IPCClient::read_handler, this, boost::asio::placeholders::error, sock));
        return true;
    }
    
    bool IPCClient::read_handler(const boost::system::error_code &ec, sock_ptr sock) {
        if (ec) {
            cout << "异步读取错误!请检查配置" << endl;
            return false;
        }
        cout<<&m_buf[0]<<endl;
        return true;
    }

     服务端:

    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/asio.hpp>
    #include <boost/asio/placeholders.hpp>
    #include <boost/system/error_code.hpp>
    #include <boost/bind/bind.hpp>
    
    class IPCServer {
    public:
        IPCServer();
        ~IPCServer();
        bool run();
    
    private:
        void accept_handler(const boost::system::error_code& ec, boost::shared_ptr<boost::asio::ip::tcp::socket> sock);
        bool accept();
        void write_handler(const boost::system::error_code&ec);
        bool initAsync();
        bool send();
        bool recv();
    
    private:
        boost::asio::io_service m_io;
        boost::asio::ip::tcp::acceptor m_acceptor;
    };
    #include "IPCServer.h"
    using namespace std;
    using namespace boost::asio;
    typedef ip::tcp::endpoint endpoint_type;
    typedef ip::tcp::socket socket_type;
    typedef boost::shared_ptr<socket_type> sock_ptr;
    
    
    IPCServer::IPCServer():m_acceptor(m_io, endpoint_type (ip::tcp::v4(), 6666)) {
        accept();
    }
    
    bool IPCServer::accept() {
        cout << "正在监听:" << m_acceptor.local_endpoint().address() << ":" << m_acceptor.local_endpoint().port() << endl;
        sock_ptr sock(new socket_type(m_io));
        m_acceptor.async_accept(*sock, boost::bind(&IPCServer::accept_handler, this, boost::asio::placeholders::error, sock));
        return true;
    }
    
    void IPCServer::accept_handler(const boost::system::error_code& ec, sock_ptr sock) {
        if (ec){
            cout << "异步接收错误!请检查配置" << endl;
            return;
        }
        cout << "客户端:";
        cout<<sock->remote_endpoint().address() << ":" << sock->remote_endpoint().port() <<endl;
        sock->async_write_some(buffer("这是从服务端发送过来的异步消息!- Yaowen Xu"), boost::bind(&IPCServer::write_handler, this, boost::asio::placeholders::error));
    }
    
    void IPCServer::write_handler(const boost::system::error_code&ec)
    {
        if (ec) {
            cout << "异步写入错误!请检查配置" << endl;
        }
        cout<<"消息发送完毕"<<endl;
    }
    
    IPCServer::~IPCServer() = default;
    
    bool IPCServer::send() {
        return false;
    }
    
    bool IPCServer::recv() {
        return false;
    }
    
    bool IPCServer::initAsync() {
        return true;
    }
    
    bool IPCServer::run() {
        m_io.run();
        return true;
    }

    保持更新,转载请注明出处。

  • 相关阅读:
    harmonOS分布式软总线
    harmonyOS的Ability是什么(一)FA的Page跳转和迁移
    转 微信支付商户如何处理微信申请退款、退款回调、查询退款接口
    转 Shiro笔记
    sql语句还能这么写(mysql脚步编写)
    Mac Shell 操作
    JSON.stringify 深拷贝的弊端以及常用场景
    elementplus elementui 官网文档如何查看
    QtQt使用QWebsocket实现服务端和客户端通信
    QtQt使用QRegExp实现正则表达式处理(Qt5.14.2+win10)
  • 原文地址:https://www.cnblogs.com/xuyaowen/p/boost-asio-socket.html
Copyright © 2020-2023  润新知