用ASIO编写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, Version 1.0.
  9. // (See accompanying
  10. // file LICENSE_1_0.txt or
  11. // copy at http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13.  
  14. #include <ctime>
  15. #include <iostream>
  16. #include <string>
  17. #include <boost/array.hpp>
  18. #include <boost/asio.hpp>
  19.  
  20. using boost::asio::ip::udp;
  21.  
  22. std::string make_daytime_string()
  23. {
  24.   using namespace std; // For time_t, time and ctime;
  25.   time_t now = time(0);
  26.   return ctime(&now);
  27. }
  28.  
  29. int main()
  30. {
  31.   try
  32.    {
  33.      boost::asio::io_service io_service;
  34.     // 在本机13端口建立一个socket
  35.      udp::socket socket(io_service, udp::endpoint(udp::v4(), 13));
  36.  
  37.     for (;;)
  38.      {
  39.        boost::array<char, 1> recv_buf;
  40.        udp::endpoint remote_endpoint;
  41.        boost::system::error_code error;
  42.       // 接收一个字符,这样就得到了远程端点(remote_endpoint)
  43.        socket.receive_from(boost::asio::buffer(recv_buf),
  44.            remote_endpoint, 0, error);
  45.  
  46.       if (error && error != boost::asio::error::message_size)
  47.         throw boost::system::system_error(error);
  48.  
  49.        std::string message = make_daytime_string();
  50.       // 向远程端点发送字符串message(当前时间)    
  51.        boost::system::error_code ignored_error;
  52.        socket.send_to(boost::asio::buffer(message),
  53.            remote_endpoint, 0, ignored_error);
  54.      }
  55.    }
  56.   catch (std::exception& e)
  57.    {
  58.      std::cerr << e.what() << std::endl;
  59.    }
  60.  
  61.   return 0;
  62. }

 

客户端:

 

  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, Version 1.0.
  9. // (See accompanying file LICENSE_1_0.txt or
  10. //   copy at http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12.  
  13. #include <iostream>
  14. #include <boost/array.hpp>
  15. #include <boost/asio.hpp>
  16.  
  17. using boost::asio::ip::udp;
  18.  
  19. int main(int argc, char* argv[])
  20. {
  21.   try
  22.    {
  23.     if (argc != 2)
  24.      {
  25.        std::cerr << "Usage: client <host>" << std::endl;
  26.       return 1;
  27.      }
  28.  
  29.      boost::asio::io_service io_service;
  30.     // 取得命令行参数对应的服务器端点
  31.      udp::resolver resolver(io_service);
  32.      udp::resolver::query query(udp::v4(), argv[1], "daytime");
  33.      udp::endpoint receiver_endpoint = *resolver.resolve(query);
  34.  
  35.      udp::socket socket(io_service);
  36.      socket.open(udp::v4());
  37.     // 发送一个字节给服务器,让服务器知道我们的地址
  38.      boost::array<char, 1> send_buf   = { 0 };
  39.      socket.send_to(boost::asio::buffer(send_buf), receiver_endpoint);
  40.     // 接收服务器发来的数据
  41.      boost::array<char, 128> recv_buf;
  42.      udp::endpoint sender_endpoint;
  43.     size_t len = socket.receive_from(
  44.          boost::asio::buffer(recv_buf), sender_endpoint);
  45.  
  46.      std::cout.write(recv_buf.data(), len);
  47.    }
  48.   catch (std::exception& e)
  49.    {
  50.      std::cerr << e.what() << std::endl;
  51.    }
  52.  
  53.   return 0;
  54. }

 



文章来自: 本站原创
Tags:
评论: 0 | 查看次数: 11345