客户端:

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(, ), m_ep(address_type::from_string("127.0.0.1"), ) {
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[]<<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(), )) {
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;
}

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

最新文章

  1. ASP.NET Core的配置(3): 将配置绑定为对象[上篇]
  2. 实战Ubuntu Server上配置LXDE+VNC环境
  3. Zip it
  4. hdu 1202 The calculation of GPA
  5. 解决DatePicker中Appbar icon缺失
  6. JAVA类与对象(七)------继承
  7. Node.js快速入门
  8. Apache Shiro 使用手冊 链接文件夹整理
  9. MCS-51单片机的指令时序
  10. 为MyEclipse加入自己定义凝视
  11. Docker和DevOps是找工作必备技能
  12. extjs 4.0.7 Desktop 替换claaes.js
  13. iOS第三方库
  14. html5中拨打电话代码
  15. 必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse。解决方法
  16. 真实世界的脉络].(英)戴维.多伊奇.pdf
  17. sqlyog数据库管理软件下载
  18. Mybatis中的延迟加载的使用方法
  19. springboot手动配置数据源:
  20. Flutter知识点:数据存储之sqflite

热门文章

  1. Python中的可变、不可变对象和赋值技巧序列解包
  2. SpringBoot之Swagger2的使用
  3. Zepto整体概况
  4. sql多表数据查询
  5. 关于Android屏幕的参数
  6. [android] 表格布局和绝对布局
  7. eclipse下svn的使用
  8. MyBatis学习笔记(二) Executor
  9. js如何发送wss协议的请求,以及接受服务器返回的数据
  10. linux mail操作