1. 程式人生 > >boost::asio 使用例項

boost::asio 使用例項

 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 }