boost之网络通信
ip::tcp的内部类型socket,acceptor以及resolver是TCP通信中最核心的类。
1.同步客户端代码:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ip/address.hpp>
using namespace std;
using namespace boost::asio; using namespace boost; int main()
{
io_service ios;
cout << "client start." <<endl;
ip::tcp::socket sock(ios);
ip::tcp::endpoint ep(ip::address::from_string("192.168.1.100"),6688);
sock.connect(ep);
vector<char> str(100,0);
sock.read_some(buffer(str));
cout << "recive from" << sock.remote_endpoint().address();
cout << &str[0] <<endl;
while(1);
return 0;
}
同步服务器端代码:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ip/address.hpp>
using namespace std;
using namespace boost::asio; using namespace boost; int main()
{
io_service ios;
cout << "server start." <<endl;
//boost::asio::ip::tcp::acceptor acceptor(ios,boost::asio::ip:tcp::endpoint(boost::asio::ip::tcp::v4(),6688));
//
ip::tcp::acceptor acc(ios,ip::tcp::endpoint(ip::tcp::v4(),6688));
cout << acc.local_endpoint().address() <<endl;
while (true)
{
ip::tcp::socket sock(ios);
acc.accept(sock);
cout << sock.remote_endpoint().address() <<endl;
sock.write_some(buffer("hello asio"));
}
return 0;
}
2.异步服务器端代码,两个异步过程一个是接受连接异步处理->另外一个是写对象处理
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/ip/address.hpp>
#include <boost/bind.hpp>
using namespace std;
using namespace boost::asio; using namespace boost; class server
{
private:
io_service& ios;//引用不能拷贝
ip::tcp::acceptor acceptor;
typedef shared_ptr<ip::tcp::socket> sock_pt;
public:
server(io_service& io):ios(io),
acceptor(ios,ip::tcp::endpoint(ip::tcp::v4(),6688))
{
start();
}
void start()
{
sock_pt sock(new ip::tcp::socket(ios));
acceptor.async_accept(*sock,bind(&server::accept_handler,this,sock));
}
void accept_handler(sock_pt sock)
{
cout << "client:";
cout << sock->remote_endpoint().address() <<endl;
sock->async_write_some(buffer("hello asio"),bind(&server::write_handler,this));
}
void write_handler()
{
cout << "send msg complete." <<endl;
}
}; int main()
{
io_service ios;
cout << "server start." <<endl;
server serv(ios);
ios.run(); return 0;
}
boost之网络通信的更多相关文章
- boost::asio::ip::tcp实现网络通信的小例子
同步方式: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型. 头文件 #include <boost/asio.hpp> 名空 ...
- ros与下位机通信常用的c++ boost串口应用
一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我下载的是boost_1_6_0版本, 解压. 2. 进入解压后目录: cd boost_1_6_0, 执 ...
- ros与下位机通信常用的c++ boost串口应用--22
摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我 ...
- C++三大库boost、loki、stlport
转: STL是一个标准,各商家根据这个标准开发了各自的STL版本.而在这形形色色的STL版本中,SGI STL无疑是最引人瞩目的一个.这当然是因为这个STL产品系出名门,其设计和编写者名单中,Alex ...
- boost.ASIO-可能是下一代C++标准的网络库
曾几何时,Boost中有一个Socket库,但后来没有了下文,C++社区一直在翘首盼望一个标准网络库的出现,网络上开源的网络库也有不少,例如Apache Portable Runtime就是比较著名的 ...
- boost------asio库的使用1(Boost程序库完全开发指南)读书笔记
asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求多线程和锁定,有效地避免了多线程编程带来的诸多有害副作用. 目前asio ...
- boost------asio库的使用2(Boost程序库完全开发指南)读书笔记
网络通信 asio库支持TCP.UDP.ICMP通信协议,它在名字空间boost::asio::ip里提供了大量的网络通信方面的函数和类,很好地封装了原始的Berkeley Socket Api,展现 ...
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- Boost程序库完全开发指南——深入C++“准”标准库(第3版)
内容简介 · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...
随机推荐
- unity, UGUI Text fadeIn
错误写法: Color color = m_text.GetComponent<Text> ().color; Color startColor = new Color (c ...
- 【SSH三大框架】Hibernate基础第九篇:cascade关联关系的级联操作
这里要说的是Hibernate的关联关系的级联操作,使用cascade属性控制. 依旧用部门和员工举例.多个员工相应一个部门(多对一关联关系) 员工类:Employee.java package cn ...
- 基于FPGA实现的高速串行交换模块实现方法研究
基于FPGA实现的高速串行交换模块实现方法研究 https://wenku.baidu.com/view/9a3d501a227916888486d7ed.html
- Django中ORM介绍和字段参数
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- K均值算法-python实现
测试数据展示: #coding:utf-8__author__ = 'similarface''''实现K均值算法 算法摘要:-----------------------------输入:所有数据点 ...
- Harmonic Number 求Hn; Hn = 1 + 1/2 + 1/3 + ... + 1/n; (n<=1e8) T<=1e4; 精确到1e-8; 打表或者调和级数
/** 题目:Harmonic Number 链接:https://vjudge.net/contest/154246#problem/I 题意:求Hn: Hn = 1 + 1/2 + 1/3 + . ...
- Git使用技巧(3)-- 远程操作
Git远程操作详解 作者: 阮一峰 编辑更新:shifu204 日期: 2016年9月1日 Git是目前最流行的版本管理系统,学会Git几乎成了开发者的必备技能. Git有很多优势,其中之一就是远程操 ...
- datagrid返回记录为0时显示“没有记录”
datagrid返回记录为0时显示“没有记录”,此问题的 <script>var myview = $.extend({},$.fn.datagrid.defaults.view,{ on ...
- day7笔记
一.上节回顾 字典:dic = {'name':'alex'} 1,增 dic['k'] = 'v' 有键值对,则覆盖 setdefault 有键值对,不添加 dic.setdefault('k1', ...
- R-ArcGIS探秘(1)安装以及Sample执行
在今年的全球用户大会上,Esri官方发布了R-ArcGIS的官方演示样例,在ArcMap和ArcGIS pro中,直接通过Toolbox能够调用R的分析工具包,将R的分析能力直接作用在ArcGIS上面 ...