[boost::asio学习 03]用ASIO编写UDP通信程序

http://blog.sina.com.cn/s/blog_78b3a4ff0100u8rz.html

 UDP协议

ASIO的TCP协议通过boost::asio::ip名空间下的tcp类进行通信,举一返三:ASIO的UDP协议通过boost::asio::ip名空间下的udp类进行通信。

我们知道UDP是基于数据报模式的,所以事先不需要建立连接。就象寄信一样,要寄给谁只要写上地址往门口的邮箱一丢,其它的事各级邮局包办;要收信用只要看看自家信箱里有没有信件就行(或问门口传达室老大爷)。在ASIO里,就是udp::socketsend_toreceive_from方法(异步版本是async_send_to和asnync_receive_from)。

下面的示例代码是从ASIO官方文档里拿来的(实在想不出更好的例子了:-P)

服务器端代码

  1. //
  2. //server.cpp
  3. //~~~~~~~~~~
  4. //
  5. //Copyright (c) 2003-2008 Christopher M. Kohlhoff
  6. //(chris at kohlhoff dot com)
  7. //
  8. //Distributed under the Boost Software License, Version1.0.
  9. //(See accompanying
  10. //file LICENSE_1_0.txt or
  11. //copy at http://www.boost.org/LICENSE_1_0.txt)
  12. //

  13. #include
  14. #include
  15. #include
  16. #include
  17. #include

  18. usingboost::asio::ip::udp;

  19. std::stringmake_daytime_string()
  20. {
  21.   using namespace std; // Fortime_t, time and ctime;
  22.   time_t now = time(0);
  23.   return ctime(&now);
  24. }

  25. int main()
  26. {
  27.   try
  28.   {
  29.     boost::asio::io_service io_service;
  30.     //在本机13端口建立一个socket
  31.     udp::socket socket(io_service, udp::endpoint(udp::v4(),13));

  32.     for(;;)
  33.     {
  34.       boost::array<char, 1> recv_buf;
  35.       udp::endpoint remote_endpoint;
  36.       boost::system::error_code error;
  37.       //接收一个字符,这样就得到了远程端点(remote_endpoint)
  38.       socket.receive_from(boost::asio::buffer(recv_buf),
  39.           remote_endpoint, 0, error);

  40.       if(error&& error !=boost::asio::error::message_size)
  41.         throwboost::system::system_error(error);

  42.       std::string message = make_daytime_string();
  43.       //向远程端点发送字符串message(当前时间)    
  44.       boost::system::error_code ignored_error;
  45.       socket.send_to(boost::asio::buffer(message),
  46.           remote_endpoint, 0, ignored_error);
  47.     }
  48.   }
  49.   catch (std::exception&e)
  50.   {
  51.     std::cerr << e.what()<< std::endl;
  52.   }

  53.   return 0;
  54. }

客户端代码

  1. //
  2. //client.cpp
  3. //~~~~~~~~~~
  4. //
  5. //Copyright (c) 2003-2008 Christopher M. Kohlhoff
  6. //(chris at kohlhoff dot com)
  7. //
  8. //Distributed under the Boost Software License, Version1.0.
  9. //(See accompanying file LICENSE_1_0.txt or
  10. //   copyat http://www.boost.org/LICENSE_1_0.txt)
  11. //

  12. #include
  13. #include
  14. #include

  15. usingboost::asio::ip::udp;

  16. int main(int argc, char* argv[])
  17. {
  18.   try
  19.   {
  20.     if(argc != 2)
  21.     {
  22.       std::cerr << "Usage: client" <
  23.       return1;
  24.     }

  25.     boost::asio::io_service io_service;
  26.     //取得命令行参数对应的服务器端点
  27.     udp::resolver resolver(io_service);
  28.     udp::resolver::query query(udp::v4(), argv[1], "daytime");
  29.     udp::endpoint receiver_endpoint =*resolver.resolve(query);

  30.     udp::socket socket(io_service);
  31.     socket.open(udp::v4());
  32.     //发送一个字节给服务器,让服务器知道我们的地址
  33.     boost::array<char, 1>send_buf   = { 0};
  34.     socket.send_to(boost::asio::buffer(send_buf),receiver_endpoint);
  35.     //接收服务器发来的数据
  36.     boost::array<char, 128> recv_buf;
  37.     udp::endpoint sender_endpoint;
  38.     size_tlen =socket.receive_from(
  39.         boost::asio::buffer(recv_buf), sender_endpoint);

  40.     std::cout.write(recv_buf.data(), len);
  41.   }
  42.   catch (std::exception&e)
  43.   {
  44.     std::cerr << e.what()<< std::endl;
  45.   }

  46.   return 0;
  47. }

你可能感兴趣的:(BOOST,C++)