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. SGDMA

    Scatter-gather DMA 使用一个链表描述物理上不连续的存储空间,然后把链表首地址告诉DMA master.DMA master在传输完一块物理连续的数据后,不用发起中断,而是根据链表来传 ...

  2. python统计订单走势

    #coding=utf-8 import numpy as np import pandas as pd import matplotlib.pyplot as plt import matplotl ...

  3. java获取真实的ip地址

    直接上代码,获取请求主机的IP地址,如果通过代理进来,则透过防火墙获取真实IP地址 public class IPUtil { private static final Logger logger = ...

  4. MVC4的缓存

    目录(?)[+] MVC3缓存之一:使用页面缓存 在MVC3中要如果要启用页面缓存,在页面对应的Action前面加上一个OutputCache属性即可. 我们建一个Demo来测试一下,在此Demo中, ...

  5. Leetcode392. Is Subsequence

    Description Given a string s and a string t, check if s is subsequence of t. You may assume that the ...

  6. eclipse启动不起来,eclipse.ini配置问题(支持大内存64bit问题)

    Eclipse 启动不起来异常:JVM terminated. Exit code=-1 Eclipse 启动不起来,出现以下错误: JVM terminated. Exit code=-1-Xms4 ...

  7. AI for AI

    1.Li, Ke, and Jitendra Malik. "Learning to optimize." arXiv preprint arXiv:1606.01885 (201 ...

  8. ios --也是在B页面的生命周期设置如下代码。方法一是直接关闭和激活侧滑手势,方法二则是B遵循协议UIGestureRecognizerDelegate,设置侧滑交互代理,重写手势方法。

    @property (weak, nonatomic) id<UIGestureRecognizerDelegate> restoreInteractivePopGestureDelega ...

  9. SAP ABAP 常见系统变量

    因为版权的问题,纯手工打,且行且珍惜. SY-PAGEO  当前页号 SY-LSIND 列表索引页 SY-DATUM 当前日期 SY-UZEIT 当前时间 SY-LISTI 上一个列表的索引 SY-L ...

  10. 定位frame中的元素

    场景 处理frame需要用到2个方法,分别是switch_to_frame(name_or_id_or_frame_element)和switch_to_default_content() 如何理解这 ...