设为首页 加入收藏

TOP

boost------asio库的使用2(Boost程序库完全开发指南)读书笔记 (六)
2015-11-21 01:30:57 来源: 作者: 【 】 浏览:42
Tags:boost------asio 使用 Boost 程序 完全 开发指南 读书 笔记
;?
?
??????? // 异步读取数据??
??????? sock->async_read_some(boost::asio::buffer(*str), boost::bind(&Client::read_handle,?
??????????????????????????????????????????????????????????????????? this,??
??????????????????????????????????????????????????????????????????? boost::asio::placeholders::error,?
??????????????????????????????????????????????????????????????????? str));?
??????? Start(); // 再次启动异步连接??
??? }?
?
??? void read_handle(const boost::system::error_code& error,??
???????????????????? boost::shared_ptr > str)?
??? {?
??????? if (error)?
??????? {?
??????????? return;?
??????? }?
??????? cout << &(*str)[0] << endl;?
??? }?
};?
?
?
int _tmain(int argc, _TCHAR* argv[])?
{?
??? try?
??? {?
??????? cout << "client start." << endl;?
??????? boost::asio::io_service ios;?
?
??????? Client client(ios);?
?
??????? ios.run();?
??? }?
??? catch (std::exception& e)?
??? {?
??????? cout << e.what() << endl;?
??? }?
?
??? 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 Client
{
private:
?boost::asio::io_service& ios;
?boost::asio::ip::tcp::endpoint ep;?// tcp端点
?typedef boost::shared_ptr sock_pt;

public:
?Client(boost::asio::io_service& io) : ios(io),
??ep(boost::asio::ip::address::from_string("127.0.0.1"), 6688)
?{
??Start(); // 启动异步连接
?}

?~Client()
?{

?}

?void Start()
?{
??sock_pt sock(new boost::asio::ip::tcp::socket(ios));
??sock->async_connect(ep, boost::bind(&Client::conn_handle, this,
???????????boost::asio::placeholders::error, sock));
?}

?void conn_handle(const boost::system::error_code& error, sock_pt sock)
?{
??if (error)
??{
???return;
??}
??cout << "recive from : " << sock->remote_endpoint().address();

??// 建立接收数据的缓冲区
??boost::shared_ptr > str(new vector(100, 0));

??// 异步读取数据
??sock->async_read_some(boost::asio::buffer(*str), boost::bind(&Client::read_handle,
?????????????????this,
?????????????????boost::asio::placeholders::error,
?????????????????str));
??Start(); // 再次启动异步连接
?}

?void read_handle(const boost::system::error_code& error,
????? boost::shared_ptr > str)
?{
??if (error)
??{
???return;
??}
??cout << &(*str)[0] << endl;
?}
};


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

??Client client(ios);

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

?return 0;
}

?

?

4、查询网络地址
之前关于tcp通信的所有论述都是使用直接的ip地址,但在实际生活中大多数时候,都不大可能知道socket链接另一端的地址,而只有一个域名,这时候我们就需要使用resolver类来通过域名获得可用的ip,它可以实现与ip版本无关的网址解析

?


resolver使用内部类query和iterator共同完成查询ip地址的工作:首先使用网址和服务名创建query对象,然后由resolve()函数生成iterator对象,它代表了查询到的ip端点。之后就可以使用socket对象尝试连接,知道找到一个可用的为止。

[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 "boost/lexical_cast.hpp"??
#include "boost/asio/error.hpp"??
#include "iostream"??
using namespace std;?
?
?
void resolv_connect(boost::asio::ip::tcp::socket& sock, const char* name, int port)?
{?
??? boost::asio::ip::tcp::resolver rlv(sock.get_io_service());?
??? boost::asio::ip::tcp::resolver::query qry(name, boost::lexical_cast(port));?
?
??? boost::asio::ip::tcp::resolver::iterator iter = rlv.resolve(qry);?
??? boost::asio::ip::tcp::resolver::itera

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

评论

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