设为首页 加入收藏

TOP

boost------asio库的使用2(Boost程序库完全开发指南)读书笔记 (二)
2015-11-21 01:30:57 来源: 作者: 【 】 浏览:41
Tags:boost------asio 使用 Boost 程序 完全 开发指南 读书 笔记
ver.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;?
?
?
int _tmain(int argc, _TCHAR* argv[])?
{?
??? try?
??? {?
??????? cout << "server start" << endl;?
??????? boost::asio::io_service ios;?
?
??????? boost::asio::ip::tcp::acceptor acceptor(ios,??
??????????? boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6688));?
?
??????? cout << acceptor.local_endpoint().address() << endl;?
?
??????? while (true)?
??????? {?
??????????? boost::asio::ip::tcp::socket sock(ios);?
??????????? acceptor.accept(sock);?
?
??????????? cout << "client : ";?
??????????? cout << sock.remote_endpoint().address() << endl;?
?
??????????? sock.write_some(boost::asio::buffer("hello asio"));?
??????? }?
??? }?
?
??? catch (std::exception& e)?
??? {?
??????? cout << e.what() << endl;?
??? }?
?
??? return 0;?
}?

// 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;


int _tmain(int argc, _TCHAR* argv[])
{
?try
?{
??cout << "server start" << endl;
??boost::asio::io_service ios;

??boost::asio::ip::tcp::acceptor acceptor(ios,
???boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 6688));

??cout << acceptor.local_endpoint().address() << endl;

??while (true)
??{
???boost::asio::ip::tcp::socket sock(ios);
???acceptor.accept(sock);

???cout << "client : ";
???cout << sock.remote_endpoint().address() << endl;

???sock.write_some(boost::asio::buffer("hello asio"));
??}
?}

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

?return 0;
}
服务器端程序里要注意的是自由函数buffer(),他可以包装很多种类的容器成为asio组件可用的缓冲区类型。通常不能直接把数组、vercor等容器用作asio的读写参数,必须使用buffer()函数包装

?


client:

[cpp]
// 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

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

评论

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