可以先了解一下Boost asio基本概念,以下是Boost asio实现的异步TCP/IP通信:

  服务器:

#include "stdafx.h"
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/system/error_code.hpp>
#include <boost/bind/bind.hpp> using namespace boost::asio;
using namespace std; class server
{
typedef server this_type;
typedef ip::tcp::acceptor acceptor_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::tcp::socket socket_type;
typedef ip::address address_type;
typedef boost::shared_ptr<socket_type> sock_ptr; private:
io_service m_io;
acceptor_type m_acceptor; public:
server() : m_acceptor(m_io, endpoint_type(ip::tcp::v4(), ))
{ accept(); } void run(){ m_io.run();} void accept()
{
sock_ptr sock(new socket_type(m_io));
m_acceptor.async_accept(*sock, boost::bind(&this_type::accept_handler, this, boost::asio::placeholders::error, sock));
} void accept_handler(const boost::system::error_code& ec, sock_ptr sock)
{
if (ec)
{ return; } cout<<"Client:";
cout<<sock->remote_endpoint().address()<<endl;
sock->async_write_some(buffer("hello asio"), boost::bind(&this_type::write_handler, this, boost::asio::placeholders::error));
// 发送完毕后继续监听,否则io_service将认为没有事件处理而结束运行
accept();
} void write_handler(const boost::system::error_code&ec)
{
cout<<"send msg complete"<<endl;
}
}; int main()
{
try
{
cout<<"Server start."<<endl;
server srv;
srv.run();
}
catch (std::exception &e)
{
cout<<e.what()<<endl;
} return ;
}

  客户端:

#include "stdafx.h"
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/system/error_code.hpp>
#include <boost/bind/bind.hpp> using namespace boost::asio;
using namespace std; class client
{
typedef client this_type;
typedef ip::tcp::acceptor acceptor_type;
typedef ip::tcp::endpoint endpoint_type;
typedef ip::tcp::socket socket_type;
typedef ip::address address_type;
typedef boost::shared_ptr<socket_type> sock_ptr;
typedef vector<char> buffer_type; private:
io_service m_io;
buffer_type m_buf;
endpoint_type m_ep;
public:
client(): m_buf(, ), m_ep(address_type::from_string("127.0.0.1"), )
{ start(); } void run()
{ m_io.run();} void start()
{
sock_ptr sock(new socket_type(m_io));
sock->async_connect(m_ep, boost::bind(&this_type::conn_handler, this, boost::asio::placeholders::error, sock));
} void conn_handler(const boost::system::error_code&ec, sock_ptr sock)
{
if (ec)
{return;}
cout<<"Receive from "<<sock->remote_endpoint().address()<<": "<<endl;
sock->async_read_some(buffer(m_buf), boost::bind(&client::read_handler, this, boost::asio::placeholders::error, sock));
} void read_handler(const boost::system::error_code&ec, sock_ptr sock)
{
if (ec)
{return;}
sock->async_read_some(buffer(m_buf), boost::bind(&client::read_handler, this, boost::asio::placeholders::error, sock));
cout<<&m_buf[]<<endl;
}
}; int main()
{
try
{
cout<<"Client start."<<endl;
client cl;
cl.run();
}
catch (std::exception &e)
{
cout<<e.what()<<endl;
} return ;
}

使用Boost asio实现异步的TCP/IP通信的更多相关文章

  1. 使用Boost asio实现同步的TCP/IP通信

    可以先了解一下Boost asio基本概念,以下是Boost asio实现的同步TCP/IP通信: 服务器程序部分,如果想保留套接字之后继续通信,可以动态申请socket_type,保存指针,因为so ...

  2. TCP/IP 通信

    TCP/IP 通信又叫socket 通信,是基于TCP/IP协调面向连接的一个数据传输技术.是属于OSI国际标准的传输层,三次握手 提供数据,有序,安全,端到端的传输和接收.它有三个主要协议:传输控制 ...

  3. tcp/ip通信第5期之服务器端程序

    /* 此程序是tcp/ip通信服务器端程序,测试运行在redhat5上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> # ...

  4. tcp/ip通信第5期之客户机端程序

    /*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...

  5. 第4章 TCP/IP通信案例:访问Internet上的Web服务器

    第4章 TCP/IP通信案例:访问Internet上的Web服务器 4.2 部署代理服务器 书中为了演示访问Internet上的Web服务器的全过程,使用了squid代理服务器程序模拟了一个代理服务器 ...

  6. TCP/IP通信过程(以发送电子邮件为例)(转)

    1.应用程序处理 (1)A用户启动邮件应用程序,填写收件人邮箱和发送内容,点击“发送”,开始TCP/IP通信: (2)应用程序对发送的内容进行编码处理,这一过程相当于OSI的表示层功能: (3)由A用 ...

  7. linux高性能服务器编程 (四) --TCP/IP通信案例

    第四章 TCP/IP通信案例 HTTP代理服务器的大致工作原理        在HTTP通信链上,客户端和服务器之间通常存在某些中转代理服务器.它们提供对目标资源的中转访问.一个HTTP请求可能被多个 ...

  8. TCP/IP通信网络基础

    TCP/IP是互联网相关的各类协议族的总称. TCP/IP的分层管理 分层的优点:如果只有一个协议在互联网上统筹,某个地方修改就要把所有的部分整体换掉,采用分层则只需要改变相应的层.把各个接口部分规划 ...

  9. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

随机推荐

  1. linux tail命令的使用方法详解 (转载)

    本文介绍Linux下tail命令的使用方法.linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新 ...

  2. c++中智能输出文件

    首先我们要为每一时间步,设置一个文件名: ] = "; itoa(time,timestr,); std::string s; s += timestr; std::string path ...

  3. 解决xp搜索“文件中的一个字或者词组”失效

    问:我的电脑安装的是Windows XP系统,最近它的文件搜索功能不能用了,打开搜索界面时,输入文件或文件夹名的文本框是灰色的,无法输入.请问该怎么解决? 答:打开注册表编辑器,定位到[HKEY_CU ...

  4. iOS10适配知识点

    http://ios.jobbole.com/89551/ http://ios.jobbole.com/88982/ 2.隐私数据访问问题 问题出现 现在app能运行了,当我打开相机时突然又cras ...

  5. Codeforces Round #342 (Div. 2) A - Guest From the Past 数学

    A. Guest From the Past 题目连接: http://www.codeforces.com/contest/625/problem/A Description Kolya Geras ...

  6. cdoj 30 最短路 flyod

    最短路 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/30 Descript ...

  7. Android学习笔记(四十):Preference的使用

    Preference直译为偏好,博友建议翻译为首选项.一些配置数据,一些我们上次点击选择的内容,我们希望在下次应用调起的时候依旧有效,无须用户再一次进行配置或选择.Android提供preferenc ...

  8. 微软免费TFS如何设置在客户端独占签出

    最近发现微软给我们提供了免费的TFS,地址:http://tfs.visualstudio.com/, 就注册了一个,但是我发现没办法独占签出. 在公司里,TFS有服务端,所以很好设置,但是注册微软的 ...

  9. vm内核参数优化设置

     http://www.cnblogs.com/wjoyxt/archive/2014/06/08/3777042.html (1)vm.overcommit_memory 执行grep -i com ...

  10. Linux网卡高级命令、IP别名及多网卡绑定 转

    http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/26/3100065.html 本篇随笔将详细讲解Linux系统的网卡高级命令.IP别名 ...