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之网络通信的更多相关文章

  1. boost::asio::ip::tcp实现网络通信的小例子

    同步方式: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型. 头文件 #include <boost/asio.hpp> 名空 ...

  2. ros与下位机通信常用的c++ boost串口应用

    一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我下载的是boost_1_6_0版本, 解压. 2. 进入解压后目录: cd boost_1_6_0, 执 ...

  3. ros与下位机通信常用的c++ boost串口应用--22

    摘要: 原创博客:转载请表明出处:http://www.cnblogs.com/zxouxuewei/ 一.首先移植c++ boost 库: 1. 先去 Boost官网 下载最新的Boost版本, 我 ...

  4. C++三大库boost、loki、stlport

    转: STL是一个标准,各商家根据这个标准开发了各自的STL版本.而在这形形色色的STL版本中,SGI STL无疑是最引人瞩目的一个.这当然是因为这个STL产品系出名门,其设计和编写者名单中,Alex ...

  5. boost.ASIO-可能是下一代C++标准的网络库

    曾几何时,Boost中有一个Socket库,但后来没有了下文,C++社区一直在翘首盼望一个标准网络库的出现,网络上开源的网络库也有不少,例如Apache Portable Runtime就是比较著名的 ...

  6. boost------asio库的使用1(Boost程序库完全开发指南)读书笔记

    asio库基于操作系统提供的异步机制,采用前摄器设计模式(Proactor)实现了可移植的异步(或者同步)IO操作,而且并不要求多线程和锁定,有效地避免了多线程编程带来的诸多有害副作用. 目前asio ...

  7. boost------asio库的使用2(Boost程序库完全开发指南)读书笔记

    网络通信 asio库支持TCP.UDP.ICMP通信协议,它在名字空间boost::asio::ip里提供了大量的网络通信方面的函数和类,很好地封装了原始的Berkeley Socket Api,展现 ...

  8. boost::asio译文

        Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...

  9. Boost程序库完全开发指南——深入C++“准”标准库(第3版)

    内容简介  · · · · · · Boost 是一个功能强大.构造精巧.跨平台.开源并且完全免费的C++程序库,有着“C++‘准’标准库”的美誉. Boost 由C++标准委员会部分成员所设立的Bo ...

随机推荐

  1. object is not an instance of declaring class

    错误原因:invoke方法的时候,应该是类的实例对象,而不是类本身 解决方法:把 PowerMockito.doReturn(index_expect).when(IndexController.cl ...

  2. iOS 学习笔记一【屏幕截图,并显示当前View】

    // 直接上代码: // // ViewController.h // 屏幕截图测试 // // Created by 博爱之家 on 15/11/11. // Copyright © 2015年 博 ...

  3. 基于AR9331(MIPS架构)分析系统启动过程(uboot)

    前提: 1.AR9331是基于MIPS 24K CPU的一款WIFI1X1芯片,其SDK采用uboot作为引导.AR9331中定义的基地址是:0x9f00,0000 2.MIPS24K芯片,将固定的起 ...

  4. hdu5794 A Simple Chess 容斥+Lucas 从(1,1)开始出发,每一步从(x1,y1)到达(x2,y2)满足(x2−x1)^2+(y2−y1)^2=5, x2>x1,y2>y1; 其实就是走日字。而且是往(n,m)方向走的日字。还有r个障碍物,障碍物不可以到达。求(1,1)到(n,m)的路径条数。

    A Simple Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  5. 创建Spring Boot项目的几种方式总结

    一.我们可以使用Spring Initializr来创建SpringBoot项目. Spring Initializr从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构.虽 ...

  6. 我的第三个java程序 两数相加

    import java.util.Scanner; public class Test { public static void main(String [] args) { Scanner sc = ...

  7. ChemDraw破解版真的不大好用

    一直以来都有很多的用户朋友在网上找ChemDraw破解版使用,但是现在厂商清理的厉害,还有国家对知识产权的保护越来越严格,破解版ChemDraw越来越难找了.大家与其花那么多的时候找破解版的,不如买个 ...

  8. week 5: ;Lasso regression & coordinate descent

    笔记. 岭回归, 计算回归系数时使( RSS(w)+λ||w||2) 最小 岭回归的结果会是所有的特征的weight都较小,但大多数又不完全为零. 而实际情况中,有的特征的确与输出值相关程度很高,we ...

  9. NDK,在JNI层使用AssetManager读取文件

    NDK,二进制文件数据读取,在JNI层,通过AAssetManager读取asset内部的资源: 需要头文件的支持 #include <android/asset_manager_jni.h&g ...

  10. 多线程编程中的join函数

    # coding: utf-8 # 测试多线程中join的功能 import threading, time def doWaiting(): print 'start waiting1: ' + t ...