随笔-4  评论-40  文章-117  trackbacks-0

boost在1.35版本之后终于加入了网络库asio。
地址:http://think-async.com/Asio/

asio的名字突出了异步I/O的能力,从asio的文档中看到它使用了和ACE Proactor框架中相似的Proactor模式。CSDN上也有很多网友也写了很多关于异步I/O的好文章,但是我还是决定从同步I/O开始。尽管阻塞I/O不是那么酷那么绚丽但是在网络编程中它和异步I/O一样重要。

下面是一个简单的同步I/O的例子,使用的是锁步(lock-step)方式的通讯。

view plaincopy to clipboardprint?
#include <string>
#include "boost/asio.hpp"
#include "boost/lexical_cast.hpp"

using namespace std;
using namespace boost;
using boost::asio::ip::tcp;
using boost::asio::io_service;

class Client
{
public:
Client (const string & hostname, unsigned short port);
virtual ~Client ();

// methods
virtual void send (const string & message);
virtual string recv ();
virtual void close ();

private:
io_service * io_service_;
tcp::socket * socket_;
};

Client::Client (const string & hostname, unsigned short port)
{
io_service_ = new io_service();
socket_ = new tcp::socket(*io_service_);

tcp::resolver resolver(*io_service_);
tcp::resolver::query query(hostname, boost::lexical_cast<string, unsigned short>(port));

boost::system::error_code ec;
tcp::resolver::iterator iter = resolver.resolve(query, ec);
tcp::resolver::iterator end;

// pick the first endpoint
if (iter != end && ec == 0)
{
tcp::endpoint endpoint = *iter;
std::cout << "Connecting to: " << endpoint << std::endl;

socket_->connect(endpoint, ec);
if (ec)
{
std::cerr << "Error: " << ec << std::endl;
throw ec;
}
}
}

Client::~Client ()
{
delete socket_;
delete io_service_;
}

void Client::send (const string & message)
{
boost::asio::const_buffers_1 request(message.data(), message.size());
socket_->send(request);
}

string Client::recv ()
{
char response[128];
size_t num = socket_->receive(boost::asio::buffer(response));
if (num > 0)
{
return string (response, num);
}

return "";
}

void Client::close ()
{
socket_->close();

}

int _tmain(int argc, _TCHAR* argv[])
{
Client client ("localhost", 2009);
std::cout << client.recv() << endl;

string request;

do
{
std::cout << "Request: ";

std::cin >> request;

if (request == "q")
break;

client.send (request);

std::cout << "Response: " << client.recv() << endl;
}
while (true);

client.close();
return 0;
}
posted on 2009-05-06 14:15 李阳 阅读(1697) 评论(0)  编辑 收藏 引用 所属分类: C++

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理