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. Webpack 的 HtmlWebpackPlugin 如何控制某个 chunks 的 inject 位置?

    https://segmentfault.com/q/1010000006591131 通过修改 HtmlWebpackPlugin 源码实现了 修改后的配置: new HtmlWebpackPlug ...

  2. 我的 Android 学习笔记-Okhttp 的使用(译)

    okhttp 已经是非常流行的网络请求库了.网上介绍的文章非常之多,但感觉都不是特别系统.遂想到官方应该有介绍的文档,仔细寻找一番,果然.但可惜是英文的,于是就大致翻译了一下,权当做笔记了. 1.Ca ...

  3. match函数

    match(s, r [, a]) Return the position in s where the regular expression r occurs, or 0 if r is not p ...

  4. Unity3D - 使用TexturePacker打包图集以及NGUI对旋转sprites的支持

    作者:EnigmaJJ 博客地址:http://www.cnblogs.com/twjcnblog/ 在Unity中使用NGUI时,为了减少draw call,我们会将美术用到的小图打成一张图集,如图 ...

  5. 计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:

    include "stdafx.h" #include <iostream> #include <fstream> #include <string& ...

  6. 【转】 Monkey测试1——Monkey的使用

    前言: 最近开始研究Android自动化测试方法,对其中的一些工具.方法和框架做了一些简单的整理,其中包括android测试框架.CTS.Monkey.Monkeyrunner.benchmark.其 ...

  7. Spring MVC隐藏字段域

    以下示例显示如何在使用Spring Web MVC框架的表单中使用隐藏字段(Hidden).首先使用Eclipse IDE来创建一个WEB工程,实现在隐藏字段中指定用户编号的功能.并按照以下步骤使用S ...

  8. Eclipse 创建 Java 包

    打开新建 Java 包向导 你可以使用新建 Java 包向导来创建 Java 包.Java 包向导打开方式有: 通过点击 "File" 菜单并选择 New > Package ...

  9. 百度map

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. Error:“const char*”类型的实参与“wchar_t”类型的形参不兼容

    MainApp\RPolarView.cpp(1571): error C2664: “ATL::CStringT<BaseType,StringTraits>::ReverseFind” ...