C++ boost::asio编程-异步TCP详解及实例代码
作者:lqh 时间:2021-12-28 03:36:52
C++ boost::asio编程-异步TCP
大家好,我是异步方式
和同步方式不同,我从来不花时间去等那些龟速的IO操作,我只是向系统说一声要做什么,然后就可以做其它事去了。如果系统完成了操作, 系统就会通过我之前给它的回调对象来通知我。
在ASIO库中,异步方式的函数或方法名称前面都有“async_ ” 前缀,函数参数里会要求放一个回调函数(或仿函数)。异步操作执行 后不管有没有完成都会立即返回,这时可以做一些其它事,直到回调函数(或仿函数)被调用,说明异步操作已经完成。
在ASIO中很多回调函数都只接受一个boost::system::error_code参数,在实际使用时肯定是不够的,所以一般 使用仿函数携带一堆相关数据作为回调,或者使用boost::bind来绑定一堆数据。
另外要注意的是,只有io_service类的run()方法运行之后回调对象才会被调用,否则即使系统已经完成了异步操作也不会有任 务动作。
好了,就介绍到这里,下面是我带来的异步方式TCP Helloworld服务器端:
// BoostTcpServer.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "boost/asio.hpp"
#include "boost/shared_ptr.hpp"
#include "boost/thread.hpp"
using namespace std;
using namespace boost::asio;
#ifdef _MSC_VER
#define _WIN32_WINNT 0X0501 //避免VC下编译警告
#endif
#define PORT 1000
#define IPV6
//#define IPV4
class AsyncServer
{
public:
//构造函数
AsyncServer(io_service &io,ip::tcp::endpoint &ep):ios(io),acceptor(io,ep)
{
//acceptor(ios,ep);
start();
}
//启动异步接受客户端连接
void start()
{
sock_ptr sock(new ip::tcp::socket(ios));
//当有连接进入时回调accept_handler函数
acceptor.async_accept(*sock,
boost::bind(&AsyncServer::accept_handler,this,placeholders::error,sock));
}
private:
io_service &ios;
ip::tcp::acceptor acceptor;
typedef boost::shared_ptr<ip::tcp::socket> sock_ptr;
void accept_handler(const boost::system::error_code &ec, sock_ptr sock)
{
if(ec)
return;
//输出客户端连接信息
std::cout <<"remote ip:"<<sock->remote_endpoint().address()<<endl;
std::cout <<"remote port:"<<sock->remote_endpoint().port() << std::endl;
//异步向客户端发送数据,发送完成时调用write_handler
sock->async_write_some(buffer("I heard you!"),
bind(&AsyncServer::write_handler,this,placeholders::error));
//再次启动异步接受连接
start();
}
void write_handler(const boost::system::error_code&)
{
cout<<"send msg complete!"<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
//定义io_service对象
io_service ios;
//定义服务端endpoint对象(协议和监听端口)
#ifdef IPV4
ip::tcp::endpoint serverep(ip::tcp::v4(),PORT);
#endif
#ifdef IPV6
ip::tcp::endpoint serverep(ip::tcp::v6(),PORT);
#endif
//启动异步服务
AsyncServer server(ios, serverep);
//等待异步完成
ios.run();
}
catch (std::exception& e)
{
cout<<e.what()<<endl;
}
return 0;
}
客户端一般无需采用异步方式,同同步方式即可。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:C++,boost::asio,异步TCP
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity使用鼠标旋转物体效果
2021-10-17 05:08:12
![](https://img.aspxhome.com/file/2023/6/68616_0s.png)
Java泛型与数据库应用实例详解
2023-08-14 09:37:15
![](https://img.aspxhome.com/file/2023/4/64014_0s.png)
轻松理解Java面试和开发中的IoC(控制反转)
2023-08-10 03:00:35
![](https://img.aspxhome.com/file/2023/4/58184_0s.jpg)
详解Spring boot/Spring 统一错误处理方案的使用
2023-11-24 12:56:07
![](https://img.aspxhome.com/file/2023/7/59897_0s.png)
配置Ant执行Jmeter脚本过程详解
2023-11-09 22:35:29
![](https://img.aspxhome.com/file/2023/5/65645_0s.png)
java中set接口使用方法详解
2023-03-15 06:10:12
SpringBoot接入支付宝支付的方法步骤
2022-02-16 05:34:16
![](https://img.aspxhome.com/file/2023/9/100049_0s.png)
Android自定义view系列之99.99%实现QQ侧滑删除效果实例代码详解
2022-05-08 15:35:55
![](https://img.aspxhome.com/file/2023/3/100593_0s.png)
JAVA IDEA 打开assert 设置方式
2022-08-19 13:48:49
![](https://img.aspxhome.com/file/2023/7/77137_0s.png)
mybatis使用Integer类型查询可能出现的问题
2022-09-01 12:47:38
详解lambda表达式foreach性能分析
2023-10-28 17:55:21
![](https://img.aspxhome.com/file/2023/1/85551_0s.jpg)
JavaWeb详细讲述Cookie和Session的概念
2022-03-23 08:39:44
![](https://img.aspxhome.com/file/2023/2/61272_0s.png)
spring aop execution表达式的用法
2023-08-22 05:39:24
![](https://img.aspxhome.com/file/2023/2/88072_0s.png)
C#实现截图工具小项目
2023-10-02 08:20:03
Android编程实现的手写板和涂鸦功能
2022-01-04 19:20:52
![](https://img.aspxhome.com/file/2023/8/101868_0s.png)
Spring创建bean实例的几种方式分享
2022-02-21 22:08:19
java synchronized 锁机制原理详解
2021-10-15 05:29:47
![](https://img.aspxhome.com/file/2023/9/64219_0s.png)
带你入门Java的泛型
2023-06-08 02:48:24
对C# 多态的理解
2022-10-17 02:37:26
C#多线程之线程控制详解
2022-02-04 17:51:44
![](https://img.aspxhome.com/file/2023/8/111078_0s.png)