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 ...
随机推荐
- vc2010, fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt解决办法
是因为安其它软件的时候更新了.net framework,导致vc2010出了问题. 解决办法是在系统里搜索cvtres.exe,会搜到很多,把其中 Microsoft Visual Studio 1 ...
- Android应用APP脱壳笔记
[TOC] 天下游 模拟定位技术点简析 通过代码分析初步猜测模拟定位用到的几处技术点: 获取了Root权限 通过反射获取 android.os.ServiceManager 对应的函数 getServ ...
- 解决带fusionCharts的页面多次点击后不显示的问题
问题: 假设不使用公司封装的fusioncharts.使用自己定义的.建议不要使用例如以下方法 使用$(document).ready( 页面载入完之后再载入,会导致多次点击带有fusionchart ...
- C#动态调用WCF接口(3)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.S ...
- 如何添加、删除、合并PDF文件里的页面?
使用Adobe Acrobat. Adobe中文官网http://www.adobe.com/cn/products/acrobat.html 能够自己下载破解版. watermark/2/text/ ...
- svn版本库目录结构
该文是svn源代码分析系列文章服务端架构中的一篇,主要描述svn服务端版本库数据存储目录结构,并且对这些文件以及目录的作用进行简单分析.使用“svnmadin create”命令创建初始化版本库后 ...
- 任何应用程序都可拥有 Web Service 组件。
任何应用程序都可拥有 Web Service 组件. Web Service 的创建与编程语言的种类无关. 本章节我们将为大家介绍使用 PHP 的 SOAP 扩展来创建 Web Service. SO ...
- ORACL EXP导出数据说明
转载自:http://www.jb51.net/article/17358.htm Oracle 数据库导出(exp)导入(imp)说明 exp 将数据库内的各对象以二进制方式下载成dmp文件,方 ...
- Wireshark使用注意事项
一直在使用老板的Wireshark,因为4G网络的逐步开通,越来越须要新版Wireshark来解析一些数据包. 在更换了新Wireshark的1.11.3后发现原来能够解析Gb口数据的NSIP不见了 ...
- Android 最新L版本,都更新什么东西了
Android L版本重大修改 一:New Android Runtime (ART) 新的运行环境,4.4一下的版本ART是可选的运行环境,默认还是Dalvik.但是在Android L版本之后默认 ...