设为首页 加入收藏

TOP

boost------asio库的使用2(Boost程序库完全开发指南)读书笔记 (四)
2015-11-21 01:30:57 来源: 作者: 【 】 浏览:33
Tags:boost------asio 使用 Boost 程序 完全 开发指南 读书 笔记
(str));?
?
??????? cout << "recive from" << sock.remote_endpoint().address();?
??????? cout << &str[0] << endl;?
?
??? }?
??? catch (std::exception& e)?
??? {?
??????? cout << e.what() << endl;?
??? }?
}?
?
?
int _tmain(int argc, _TCHAR* argv[])?
{?
??? boost::asio::io_service ios;?
??? AsynTimer at(ios, 50000, boost::bind(client, boost::ref(ios)));?
??? ios.run();?
?
??? return 0;?
}?

// client.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "boost/asio.hpp"
#include "boost/date_time/posix_time/posix_time.hpp"
#include "boost/bind.hpp"
#include "boost/function.hpp"
#include "iostream"
using namespace std;
#include "vector"


class AsynTimer
{
public:
?template????????// 模板类型,可以接受任意可调用物
?AsynTimer(boost::asio::io_service& ios, int x, F func)
??:f(func), count_max(x), count(0),????// 初始化回调函数和计数器
??t(ios, boost::posix_time::millisec(500))??// 启动计时器
?{
??t.async_wait(boost::bind(&AsynTimer::CallBack,? // 异步等待计时器
???this, boost::asio::placeholders::error));?// 注册回调函数
?}

?void CallBack(const boost::system::error_code& error)
?{
??if (count >= count_max)? // 如果计数器达到上限则返回
??{
???return;
??}
??++count;
??f();????? // 调用function对象

??// 设置定时器的终止时间为0.5秒之后
??t.expires_at(t.expires_at() + boost::posix_time::microsec(500));
??// 再次启动定时器,异步等待
??t.async_wait(boost::bind(&AsynTimer::CallBack, this, boost::asio::placeholders::error));
?}

private:
?int count;
?int count_max;
?boost::function f;??// function对象,持有无参无返回值的可调用物
?boost::asio::deadline_timer t;?// asio定时器对象
};


void client(boost::asio::io_service& ios)
{
?try
?{
??cout << "client start." << endl;

??boost::asio::ip::tcp::socket sock(ios);
??boost::asio::ip::tcp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 6688);

??sock.connect(ep);

??vector str(100, 0);
??sock.read_some(boost::asio::buffer(str));

??cout << "recive from" << sock.remote_endpoint().address();
??cout << &str[0] << endl;

?}
?catch (std::exception& e)
?{
??cout << e.what() << endl;
?}
}


int _tmain(int argc, _TCHAR* argv[])
{
?boost::asio::io_service ios;
?AsynTimer at(ios, 50000, boost::bind(client, boost::ref(ios)));
?ios.run();

?return 0;
}


3、异步socket处理
我们把刚才的同步socket程序改为异步调用方式。异步程序的处理流程与同步程序基本相同,只需要把原有的同步调用函数都换成前缀是async_的异步调用函数,并增加回调函数,在回调函数中再启动一个异步调用

?


服务器端:

[cpp]
// server.cpp : 定义控制台应用程序的入口点。??
//??
?
#include "stdafx.h"??
#include "boost/asio.hpp"??
#include "boost/date_time/posix_time/posix_time.hpp"??
#include "boost/bind.hpp"??
#include "boost/function.hpp"??
#include "iostream"??
using namespace std;?
?
?
class Server?
{?
private:?
??? boost::asio::io_service& ios;?
??? boost::asio::ip::tcp::acceptor acceptor;?
??? typedef boost::shared_ptr sock_pt;?
?
public:?
??? Server(boost::asio::io_service& io) : ios(io),?
??????? acceptor(ios, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6688))?
??? {?
??????? Start();?
??? }?
??? ~Server()?
??? {?
?
??? }?
?
??? void Start()?
??? {?
??????? sock_pt sock(new boost::asio::ip::tcp::socket(ios));??? // 智能指针??
?
??????? // 异步侦听服务??
??????? acceptor.async_accept(*sock, boost::bind(&Server::acceptor_handle,??
???????????????????????????????????????????????? this, boost::asio::placeholders::error, sock));?
??? }?
?
??? void acceptor_handle(const boost::system::error_code& error, sock_pt sock)?
??? {?
??????? if (error)?
??????? {?
??????????? return;?
??????? }?
??????? cout << "client : ";?
?
??????? // 输出连接的客户端信息??
??????? cout << sock->remote_endpoint().address() << endl;?
?
??????? //???
??????? sock->async_write_some( boost::asio::buffer("hello asio"),??
??????????????????????????????? boost::b

首页 上一页 1 2 3 4 5 6 下一页 尾页 4/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU1009 FatMouse' Trade 下一篇UVa 10905: Children's Game

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: