boost 异步socket《Boost程序完全开发指南》

异步服务器端:

 1 #include <iostream>
 2 #include <boost/asio.hpp>
 3 #include <boost/bind.hpp>
 4 #include <boost/shared_ptr.hpp>
 5 #include <boost/date_time/posix_time/posix_time.hpp>
 6 
 7 //using namespace std;
 8 using namespace boost::asio;
 9 
10 class server
11 {
12 private:
13     io_service &ios;
14     ip::tcp::acceptor acceptor;
15     typedef boost::shared_ptr<ip::tcp::socket> sock_pt;//定义了一个智能指针,指向socket对象,用来在回调函数中传递
16 public:
17     server(io_service& io):ios(io),acceptor(ios,ip::tcp::endpoint(ip::tcp::v4(),6688))
18     {start();}
19     //启动异步接受连接,调用accepter的async_accept()函数
20     void start()
21     {
22         sock_pt sock(new ip::tcp::socket(ios));
23         acceptor.async_accept(*sock,bind(&server::accept_handler,this,placeholders::error,sock));
24     }
25     //发送数据
26     void accept_handler(const boost::system::error_code & ec,sock_pt sock)
27     {
28         if(ec)
29         {
30             return;
31         }
32         std::cout<<"client:";
33         std::cout<<sock->remote_endpoint().address()<<std::endl;
34         sock->async_write_some(buffer("hello asio"),bind(&server::write_handler,this,placeholders::error));//异步发送数据,write_handler()异步调用函数
35         start();//发送完数据后,调用start()再次启动服务接受连接,否则完成数据发送后,io_service因没事件处理而结束运行。
36     }
37     //因为我们不需要再做更多的工作,可以直接实现一个空函数,这里输出一条消息
38     void write_handler(const boost::system::error_code &)
39     {
40         std::cout<<"send msg complete."<<std::endl;
41     }
42     
43 };
44 
45 
46 int main()
47 try
48 {
49     std::cout<<"server start."<<std::endl;
50     io_service ios; //io_service对象
51     server serv(ios);
52     ios.run();
53 }
54 catch(std::exception& e)
55 {
56     std::cout<<e.what()<<std::endl;
57 }

 

同步客户端:

 1 #include <iostream>
 2 #include <boost/asio.hpp>
 3 #include <vector>
 4 #include <boost/bind.hpp>
 5 #include <boost/function.hpp>
 6 using namespace std;
 7 using namespace boost;
 8 using namespace boost::asio;
 9 
10 class a_timer
11 {
12 private:
13 int count,count_max;
14 function<void()> f;
15 deadline_timer t;
16 
17 public:
18 template<typename F>
19 
20 a_timer(io_service& ios,int x,F func):f(func),count_max(x),count(0),t(ios,posix_time::millisec(500))
21 {
22     t.async_wait(bind(&a_timer::call_func,this,placeholders::error));
23 }
24 void call_func(const system::error_code&)
25 {
26     if(count>=count_max)
27     {
28         return;
29     }
30     ++count;
31     f();
32     t.expires_at(t.expires_at()+posix_time::millisec(500));
33     t.async_wait(bind(&a_timer::call_func,this,placeholders::error));
34 }
35 };
36 
37 void client(io_service & ios)
38 try
39 {
40 std::cout<<"client start."<<std::endl;
41 ip::tcp::socket sock(ios);
42 ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"),6688);
43 sock.connect(ep);
44 std::vector<char> str(100,0);
45 sock.read_some(buffer(str));
46 std::cout<<"recive from"<<sock.remote_endpoint().address();
47 std::cout<<&str[0]<<std::endl;
48 }
49 catch(std::exception& e)
50 {
51 std::cout<<e.what()<<endl;
52 }
53 
54 int main()
55 {
56 io_service ios;
57 a_timer at(ios,5,bind(client,ref(ios)));
58 ios.run();
59 }

 

posted @ 2013-06-20 19:41  星语160523  阅读(932)  评论(0编辑  收藏  举报