ind(&Server::write_handle,?
??????????????????????????????? this, boost::asio::placeholders::error));?
?
??????? Start(); // 再次启动异步接受连接??
??? }?
?
??? void write_handle(const boost::system::error_code& error)?
??? {?
??????? cout << "send message is complate" << endl;?
??? }?
?
};?
?
?
int _tmain(int argc, _TCHAR* argv[])?
{?
??? try?
??? {?
??????? cout << "server start." << endl;?
?
??????? boost::asio::io_service ios;?
?
??????? Server serv(ios);?
?
??????? ios.run();?
??? }?
??? 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;
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::bind(&Server::write_handle,
?????????this, boost::asio::placeholders::error));
??Start(); // 再次启动异步接受连接
?}
?void write_handle(const boost::system::error_code& error)
?{
??cout << "send message is complate" << endl;
?}
};
int _tmain(int argc, _TCHAR* argv[])
{
?try
?{
??cout << "server start." << endl;
??boost::asio::io_service ios;
??Server serv(ios);
??ios.run();
?}
?catch (std::exception& e)
?{
??cout << e.what() << endl;
?}
?return 0;
}
首先检查asio传递的error_code,保证没有错误发生。然后调用socket对象的async_write_some()异步发送数据。同样,我们必须再为这个异步调用编写回调函数write_handler()。当发送完数据后不要忘记调用Start()再次启动服务器接受链接,否则当完成数据发送后io_service将因为没有时间处理而结束运行。
?
发送数据的回调函数write_handler()很简单,因为不需要做更多的工作,可以直接实现一个空函数,在这里简单地输出一条信息,表示异步发送数据完成
?
客户端:
[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 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))