使用Boost asio实现异步的TCP/IP通信
可以先了解一下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通信的更多相关文章
- 使用Boost asio实现同步的TCP/IP通信
可以先了解一下Boost asio基本概念,以下是Boost asio实现的同步TCP/IP通信: 服务器程序部分,如果想保留套接字之后继续通信,可以动态申请socket_type,保存指针,因为so ...
- TCP/IP 通信
TCP/IP 通信又叫socket 通信,是基于TCP/IP协调面向连接的一个数据传输技术.是属于OSI国际标准的传输层,三次握手 提供数据,有序,安全,端到端的传输和接收.它有三个主要协议:传输控制 ...
- tcp/ip通信第5期之服务器端程序
/* 此程序是tcp/ip通信服务器端程序,测试运行在redhat5上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> # ...
- tcp/ip通信第5期之客户机端程序
/*此程序是tcp/ip通信的客户机端程序, 测试运行在redhat6系统上 重构readline函数,解决粘包问题——利用“\n”识别一个消息边界 */ #include<stdio.h> ...
- 第4章 TCP/IP通信案例:访问Internet上的Web服务器
第4章 TCP/IP通信案例:访问Internet上的Web服务器 4.2 部署代理服务器 书中为了演示访问Internet上的Web服务器的全过程,使用了squid代理服务器程序模拟了一个代理服务器 ...
- TCP/IP通信过程(以发送电子邮件为例)(转)
1.应用程序处理 (1)A用户启动邮件应用程序,填写收件人邮箱和发送内容,点击“发送”,开始TCP/IP通信: (2)应用程序对发送的内容进行编码处理,这一过程相当于OSI的表示层功能: (3)由A用 ...
- linux高性能服务器编程 (四) --TCP/IP通信案例
第四章 TCP/IP通信案例 HTTP代理服务器的大致工作原理 在HTTP通信链上,客户端和服务器之间通常存在某些中转代理服务器.它们提供对目标资源的中转访问.一个HTTP请求可能被多个 ...
- TCP/IP通信网络基础
TCP/IP是互联网相关的各类协议族的总称. TCP/IP的分层管理 分层的优点:如果只有一个协议在互联网上统筹,某个地方修改就要把所有的部分整体换掉,采用分层则只需要改变相应的层.把各个接口部分规划 ...
- boost asio 异步实现tcp通讯
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
随机推荐
- C++标准库概述 [转]
C++标准库的所有头文件都没有扩展名. C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.<cname>形式的标准头文件[<complex>例外]其内 ...
- GitHub-更新数据
1.查看代码的修改 git status //modified 标示修改的文件 //deleted标示删除的文件 // untracked files 未处理的文件 需要执行 git add方法添加上 ...
- 习总强调网络安全 ,咱们国产SSL证书必须加快普及速度
上海(2014 年 2 月 27 日)—— 央视新闻联播 27 日报道中央网络安全和信息化领导小组于当日成立的消息及习总在该小组首次会议上的重要讲话.据悉,该小组由习总任小组长,李克强.刘云山任副组长 ...
- 系列文章--SharePoint 开发教程
SharePoint 2013 图文开发系列之入门教程 学习地址:http://www.cnblogs.com/jianyus/p/3461719.html 里面有2007.2010.2013各个版本 ...
- android用NDK编译出so最简单的方法
其实只要是有个jni的文件夹,再放个Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := hell ...
- jQuery Mobile 手动显示ajax加载器,提示加载中...
在使用jQuery Mobile开发时,有时候我们需要在请求ajax期间,显示加载提示框(例如:一个旋转图片+一个提示:加载中...).这个时候,我们可以手动显示jQuery Mobile的加载器,大 ...
- CodeForces 173E Camping Groups 离线线段树 树状数组
Camping Groups 题目连接: http://codeforces.com/problemset/problem/173/E Description A club wants to take ...
- C#中Thread类中Join方法的理解(转载)
指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如 using System; namespace TestThreadJoin { class Pro ...
- AR增强现实 Augmented Reality
增强现实(Augmented Reality,简称 AR),是一种实时地计算摄影机影像的位置及角度并加上对应图像的技术,这样的技术的目标是在屏幕上把虚拟世界套在现实世界并进行互动.这样的技术最早于19 ...
- 微信公共服务平台开发(.Net 的实现)13-------网页授权(下 :C#代码的实现 )
接着上次的理论,我们这次来研究用代码实现“网页授权获取用户基本信息”,首先我们需要一个链接指向微信的授权页面,在微信开发平台中已经说了,这个链接必须在微信客户端中打开,那么我们就干脆使用文本消息来完成 ...