boost::asio学习 - 常用方法总结

域名解析 

   
     
boost::asio::io_service io_service;

tcp::resolver resolver(io_service);
tcp::resolver::query query(tcp::v4(), argv[
1 ], " daytime " );
tcp::resolver::iterator endpoint_iterator
= resolver.resolve(query);
tcp::resolver::iterator end;

tcp::socket socket(io_service);
boost::system::error_code error
= boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
std::cout
<< ( * endpoint_iterator).endpoint() << std::endl;
socket.close();
socket.connect(
* endpoint_iterator ++ , error);
}

 

客户端连接指定地址

  
    
boost::asio::ip::tcp::socket socket(io_service);
boost::asio::ip::tcp::endpoint endpoint(
boost::asio::ip::address::from_string(
" 1.2.3.4 " ), 12345 );
socket.connect(endpoint);

 

服务端侦听端口

  
    
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(),
13 ));

for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);

}

你可能感兴趣的:(boost)