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

2020-12-30 10:46:18 浏览数 (1)

 客户端:

代码语言:javascript复制
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;
};
代码语言:javascript复制
#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;
}

 服务端:

代码语言:javascript复制
#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;
};
代码语言:javascript复制
#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;
}

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

0 人点赞