首页 > 其他 > 详细

boost::asio 使用实例

时间:2014-06-10 11:55:16      阅读:463      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <boost/asio.hpp>
 3 
 4 using namespace std;
 5 using namespace boost::asio;
 6 
 7 int main()
 8 {
 9     try
10     {
11         cout << "server start." << endl;
12         io_service ios;
13 
14         ip::tcp::acceptor acc(ios,
15             ip::tcp::endpoint(ip::tcp::v4(),6688));
16 
17         cout << acc.local_endpoint().address() << endl;
18 
19         while (true)
20         {
21             ip::tcp::socket sock(ios);
22             acc.accept(sock);
23 
24             cout << "client:" ;
25             cout << sock.remote_endpoint().address() << endl;
26 
27             sock.write_some(buffer("hello asio"));
28         }
29     }
30     catch (std::exception& e)
31     {
32         cout << e.what() << endl;
33     }
34 
35     return 0;
36 }
bubuko.com,布布扣

tcp client:

bubuko.com,布布扣
 1 #include <boost/asio.hpp>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 using namespace boost::asio;
 6 
 7 void client(io_service &ios)
 8 {
 9     try
10     {
11         cout << "client start." << endl;
12 
13         ip::tcp::socket sock(ios);
14         ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"),6688);
15 
16         sock.connect(ep);
17 
18         vector<char> str(100,0);
19         sock.read_some(buffer(str));
20         cout << "receive from " << sock.remote_endpoint().address();
21         cout << &str[0] << endl;
22     }
23     catch (std::exception& e)
24     {
25         cout << e.what() << endl;
26     }
27 }
28 
29 void print(const boost::system::error_code&)
30 {
31     cout << "test wait..." << endl;
32 }
33 
34 int main()
35 {
36     io_service ios;
37     deadline_timer at(ios, boost::posix_time::seconds(5));
38     at.async_wait(print);
39 
40     cout << "it show before at exired" <<endl;
41     ios.run();
42     return 0;
43 }
bubuko.com,布布扣

 

boost::asio 使用实例,布布扣,bubuko.com

boost::asio 使用实例

原文:http://www.cnblogs.com/kernel0815/p/3779040.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!