客户端:

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;
}

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

使用 boost.asio 简单实现 异步Socket 通信的更多相关文章

  1. [Boost基础]并发编程——asio网络库——异步socket处理

    异步服务器端 #include <conio.h> #include <iostream> using namespace std; #include <boost/as ...

  2. 使用Boost asio实现异步的TCP/IP通信

    可以先了解一下Boost asio基本概念,以下是Boost asio实现的异步TCP/IP通信: 服务器: #include "stdafx.h" #include <io ...

  3. UEditor编辑器和php简单的实现socket通信

    一.UEditor编辑器 使用这个编辑器是需要先下载编辑器文件,记得下载的时候放入自己的网站中,既然是php中使用,自然我下载的就是php的UEditor编辑器了,然后是utf-8的 其实使用很简单, ...

  4. c#之异步Socket通信

    0.基于上一篇的c#之Socket(同步)通信,在几个大神评论之后,发现是有挺多地方不足的,所以写了一个改进版本的基于c#的异步Socket通信.再加深一下对Socket的使用和理解.其中客户端和服务 ...

  5. 简单的异步Socket实现——SimpleSocket_V1.1

    简单的异步Socket实现——SimpleSocket_V1.1 笔者在前段时间的博客中分享了一段简单的异步.net的Socket实现.由于是笔者自己测试使用的.写的很粗糙.很简陋.于是花了点时间自己 ...

  6. Python简易聊天工具-基于异步Socket通信

    继续学习Python中,最近看书<Python基础教程>中的虚拟茶话会项目,觉得很有意思,自己敲了一遍,受益匪浅,同时记录一下. 主要用到异步socket服务客户端和服务器模块asynco ...

  7. JAVA基础知识之网络编程——-基于AIO的异步Socket通信

    异步IO 下面摘子李刚的<疯狂JAVA讲义> 按照POSIX标准来划分IO,分为同步IO和异步IO.对于IO操作分为两步,1)程序发出IO请求. 2)完成实际的IO操作. 阻塞IO和非阻塞 ...

  8. 跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型

    作为一个呼应,写一个c++版本的同步http post客户端功能,如果你需要纯C版本,移步这里 linux下纯C简单的HTTP POST请求 客户端模型 讲解一下基本的的http post协议 通过\ ...

  9. boost asio tcp 多线程异步读写,服务器与客户端。

    // server.cpp #if 0 多个线程对同一个io_service 对象处理 用到第三方库:log4cplus, google::protobuf 用到C++11的特性,Windows 需要 ...

随机推荐

  1. #1 Python灵活技巧

    前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...

  2. JVM中垃圾收集算法总结

      通过前面的介绍我们了解了对象创建和销毁的过程.那么JVM中垃圾收集器具体对对象回收采用的是什么算法呢?本文主要记录下JVM中垃圾收集的几种算法. JVM的垃圾回收的算法 标记-清除算法(Mark- ...

  3. 第一册:lesson sixty one.

    原文: A bad cold. A:Where is Jim? B:He is in bed. A:What's the matter with him? B:He fells ill. A:He l ...

  4. 从零开始学安全(十)●TCP/IP协议栈

    局域网靠mac 地址通信

  5. [PHP] 算法-字符串的左循环的PHP实现

    汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abcXYZde ...

  6. [android] 采用服务录制电话&服务的生命周期

    根据上一节代码里,加入一个录音功能,上传到服务器,就能实现一个录制器 当手机处于通话状态时,开启录音机 获取MediaRecorder对象,通过new出来 调用MediaRecorder对象的setA ...

  7. No application encryption key has been specified.

    环境:php7.1.10laravel5.5出现: 解决:在根目录下执行: php artisan key:generate OK问题解决

  8. String为什么是不可变的?

    前几天一个面试被问到String为什么是不可变的?, 自我感觉当时回答的不太理想, 事后总结一下 不可变的是什么 我们谈论的String不可变, 指的是字符串的值不可变 例: String s = & ...

  9. 记录ThreadPoolTaskExecutor线程池的在项目中的实际应用,讲解一下线程池的配置和参数理解。

    前言:最近项目中与融360项目中接口对接,有反馈接口(也就是我们接收到请求,需要立即响应,并且还要有一个接口推送给他们其他计算结果),推送过程耗时.或者说两个接口不能是同时返回,有先后顺序. 这时我想 ...

  10. kafka指定partition的分区规则

    博客地址:https://www.cnblogs.com/gnivor/p/5318319.html