使用 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(, ), 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 通信的更多相关文章
- [Boost基础]并发编程——asio网络库——异步socket处理
异步服务器端 #include <conio.h> #include <iostream> using namespace std; #include <boost/as ...
- 使用Boost asio实现异步的TCP/IP通信
可以先了解一下Boost asio基本概念,以下是Boost asio实现的异步TCP/IP通信: 服务器: #include "stdafx.h" #include <io ...
- UEditor编辑器和php简单的实现socket通信
一.UEditor编辑器 使用这个编辑器是需要先下载编辑器文件,记得下载的时候放入自己的网站中,既然是php中使用,自然我下载的就是php的UEditor编辑器了,然后是utf-8的 其实使用很简单, ...
- c#之异步Socket通信
0.基于上一篇的c#之Socket(同步)通信,在几个大神评论之后,发现是有挺多地方不足的,所以写了一个改进版本的基于c#的异步Socket通信.再加深一下对Socket的使用和理解.其中客户端和服务 ...
- 简单的异步Socket实现——SimpleSocket_V1.1
简单的异步Socket实现——SimpleSocket_V1.1 笔者在前段时间的博客中分享了一段简单的异步.net的Socket实现.由于是笔者自己测试使用的.写的很粗糙.很简陋.于是花了点时间自己 ...
- Python简易聊天工具-基于异步Socket通信
继续学习Python中,最近看书<Python基础教程>中的虚拟茶话会项目,觉得很有意思,自己敲了一遍,受益匪浅,同时记录一下. 主要用到异步socket服务客户端和服务器模块asynco ...
- JAVA基础知识之网络编程——-基于AIO的异步Socket通信
异步IO 下面摘子李刚的<疯狂JAVA讲义> 按照POSIX标准来划分IO,分为同步IO和异步IO.对于IO操作分为两步,1)程序发出IO请求. 2)完成实际的IO操作. 阻塞IO和非阻塞 ...
- 跨平台c++/boost/asio 简单的HTTP POST请求 客户端模型
作为一个呼应,写一个c++版本的同步http post客户端功能,如果你需要纯C版本,移步这里 linux下纯C简单的HTTP POST请求 客户端模型 讲解一下基本的的http post协议 通过\ ...
- boost asio tcp 多线程异步读写,服务器与客户端。
// server.cpp #if 0 多个线程对同一个io_service 对象处理 用到第三方库:log4cplus, google::protobuf 用到C++11的特性,Windows 需要 ...
随机推荐
- Spark提高篇——RDD/DataSet/DataFrame(二)
该部分分为两篇,分别介绍RDD与Dataset/DataFrame: 一.RDD 二.DataSet/DataFrame 该篇主要介绍DataSet与DataFrame. 一.生成DataFrame ...
- 《C#并发编程经典实例》学习笔记—异步编程关键字 Async和Await
C# 5.0 推出async和await,最早是.NET Framework 4.5引入,可以在Visual Studio 2012使用.在此之前的异步编程实现难度较高,async使异步编程的实现变得 ...
- 适用于WebApi的SQL注入过滤器
开发工具:Visual Studio 2017 C#版本:C#7.1 最有效的防止SQL注入的方式是调用数据库时使用参数化查询. 但是如果是接手一个旧的WebApi项目,不想改繁多的数据库访问层的代码 ...
- [面试]中高级测试工程师必备,月薪15K+
1.你的测试职业发展是什么? 测试经验越多,测试能力越高.所以我的职业发展是需要时间积累的,一步步向着高级测试工程师奔去.而且我也有初步的职业规划,前3年积累测试经验,按如何做好测试工程师的要点去要求 ...
- Spring Bean的生命周期,《Spring 实战》书中的官方说法
连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...
- Springboot整合Websocket遇到的坑
Springboot整合Websocket遇到的坑 一.使用Springboot内嵌的tomcat启动websocket 1.添加ServerEndpointExporter配置bean @Confi ...
- 二进制安装 kubernetes 1.12(五) - 运行测试实例
检查集群状态 # 在 master 上 kubectl get node kubectl get cs 注册登录阿里云容器仓库 因国内无法获得 google 的 pause-amd64 镜像,我这里使 ...
- IP 地址基本知识
ip地址被分成了5类:A类,B类,C类,D类,E类 私有地址 只能在局域网内使用,不能在internet上使用的ip地址称为私有ip地址,私有ip地址有: 10.0.0.0-10.255.255.25 ...
- Vue基础01vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令
自学vue框架,每天记录重要的知识点,与大家分享!有不足之处,希望大家指正. 本篇将讲述:vue的基本示例,vue的双向数据绑定,vue中常见的几种用法,vue相关常见指令 前期学习基础,使用vue. ...
- Python变量和简单数据类型
变量的命名和使用 在Python中使用变量时 ,需要遵守一定的规则和指南. 变量名只能包含字母‘数字和下划线 变量名不能包含空格,但可以用下划线分割其中单词 不要将Python关键字和函数名用作变量名 ...