boost::asio中的C/S同步实例源码
近来狂热地研究boost的开发技术,现将读书笔记整理如下:
需要说明的是, 本博该专题下面关于boost的源码是采用boost1.55版本, 运行在Ubuntu 14.04 64bit下面, 使用apt包安装(非源码编译安装), 后续不再做说明.
同步socket类型的服务器源码实现:
//g++ -g sync_tcp_server.cpp -o sync_tcp_server -lboost_system
//#include <iostream>
#include <boost/asio.hpp>
#include <boost/system/error_code.hpp>using namespace std;
using namespace boost::asio;int main(){try{cout << "sync tcp server start ......" << endl;io_service ios;//server listen at 127.0.0.1:6688ip::tcp::acceptor acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688));cout << acceptor.local_endpoint().address() << endl;while(true){ip::tcp::socket sock(ios);acceptor.accept(sock);cout << "client: ";cout << sock.remote_endpoint().address() << endl;sock.write_some(buffer("hello asio"));}}catch(std::exception& e){cout << e.what() << endl;}
}
同步socket类型的客户端源码实现:
//g++ -g sync_tcp_client.cpp -o sync_tcp_client -lboost_system -lboost_date_time
//#include <iostream>
#include <boost/ref.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>using namespace boost::asio;void client(io_service &ios){try {std::cout << "sync client starting ......" << std::endl;ip::tcp::socket sock(ios);ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 6688);sock.connect(ep);std::vector<char> str(100, 0);sock.read_some(buffer(str));std::cout << "receive from: " << sock.remote_endpoint().address() << std::endl;std::cout << &str[0] << std::endl;} catch (std::exception &e) {std::cout << e.what() << std::endl;}
}class a_timer
{private:int count, count_max;boost::function<void()> f;boost::asio::deadline_timer t;public:template<typename F>a_timer(io_service &ios, int x, F func): f(func), count_max(x), count(0),t(ios, boost::posix_time::millisec(500)) {t.async_wait(boost::bind(&a_timer::call_func, this, boost::asio::placeholders::error));}void call_func(const boost::system::error_code&){if (count >= count_max) {return;}++count;f();t.expires_at(t.expires_at() + boost::posix_time::millisec(500));t.async_wait(boost::bind(&a_timer::call_func, this, boost::asio::placeholders::error));}};int main()
{io_service ios;a_timer at(ios, 5, boost::bind(client, boost::ref(ios)));ios.run();return 0;
}
运行细节:
注意所有的源码均在Ubuntu 14.04 64bit上运行测试, 比参考文献[1]中更详细更具体.
参考文献:
[1].罗剑锋, Boost程序库完全开发指南---深入C++"准"标准库
boost::asio中的C/S同步实例源码相关推荐
- boost::asio异步模式的C/S客户端源码实现
异步模式的服务器源码 //g++ -g async_tcp_server.cpp -o async_tcp_server -lboost_system //#include <iostream& ...
- 详解numpy中的array(附实例源码)
Numpy定义 NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.NumPy 通常与 Sci ...
- boost::asio中文文档
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENSE_1_ ...
- Boost中的协程—Boost.Asio中的coroutine类
Boost.Asio中有两处涉及协程,本文介绍其中的coroutine类. Boost.Asio中的stackless协程是由coroutine类和一些宏来实现的.coroutine类非常简单,包括四 ...
- c语言复制粘贴源码,c语言函数memccpy()如何复制内存中的内容实例源码介绍
c语言函数memccpy()如何复制内存中的内容实例源码介绍.引入的头文件:#include memccpy()函数定义:void * memccpy(void *dest, const void * ...
- html中超链接使用_HTML实例源码
HTML实例源码 实例一 1.要求: 知识要点: html 文档的基本结构,html 标签的书写格式.作用.用法 实训目的: 掌握 html 文档的基本结构,掌握 html 代码网页编辑的基本方法实训 ...
- Silverlight实用窍门系列:56.Silverlight中的Binding使用(一)【附带实例源码】
本文将详细讲述Silverlight中Binding,包括Binding的属性和用法,Binding的数据流向. Binding:一个完整的Binding过程是让源对象中的某个属性值通过一定流向规则进 ...
- Android研发中对String的思考(源码分析)
1.常用创建方式思考: String text = "this is a test text "; 上面这一句话实际上是执行了三件事 1.声明变量 String text; 2. ...
- 微信小程序实例源码大全demo下载
怎么本地测试微信小程序实例源码 1.下载源码 2.打开微信开发者工具 3.添加项目->选择本项目目录->编译执行 微信小程序实例源码大全 微信小程序游戏类demo:识色:从相似颜色中挑选不 ...
最新文章
- 扎克伯格亲自做了26张PPT,员工效率提10倍,已被疯狂传阅
- vim末行模式下相关操作+配置文件
- 函数指针和shellcode
- DCMTK:将显示曲线导出到文本文件
- IT工作一年的总结——来自一个小菜鸟
- UOJ #150 【NOIP2015】 运输计划
- RabbitMQ架构
- Oracle会话管理
- StanfordDB class自学笔记 (7) SQL
- 国二计算机百度云,2016Office二级全套视频教程 全国计算机二级考试Office考试视频教程...
- Windows登录密码破解
- vb常用内部函数(四):随机数函数
- storm如何部署拓扑
- fuz 2159 WuYou
- 树莓派摄像头无法识别 mmal
- Eclipse 恢复误删jar包
- CiteSpace学习笔记
- CISCO ASAv 9.15 - 体验思科上一代防火墙
- 如何使用计算机防病毒,如何使用Windows Defender在使用其他防病毒时定期扫描您的计算机 | MOS86...
- 【第三方框架】Glid4.4 完全学习(未完待续)
热门文章
- Mysql依赖库Boost的源码安装,linux下boost库的安装
- 给同一个按钮添加单双击事件
- python+opencv选出视频中一帧再利用鼠标回调实现图像上画矩形框
- Linux查看目录挂载点
- xbmc-12.0稳定版代码初探 (2) —— XBMC_HOME
- usaco Drainage Ditches(网络流dinic模板)
- sublime python配置_sublime python环境配置
- php pdo 中文乱码,php pdo oracle中文乱码的快速解决方法
- android7.0图标反馈,android7.0 webview中 图标错位
- sar sensor传感器的作用_传感器攻防战-惯导IMU