现在的位置: 首页 > 综合 > 正文

用ASIO编写UDP通信程序

2013年07月16日 ⁄ 综合 ⁄ 共 2777字 ⁄ 字号 评论关闭

 

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. #include <ctime>
  14. #include <iostream>
  15. #include <string>
  16. #include <boost/array.hpp>
  17. #include <boost/asio.hpp>
  18. using boost::asio::ip::udp;
  19. std::string make_daytime_string()
  20. {
  21.   using
    namespace std;
    // For time_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.         throw boost::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, 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. #include <iostream>
  13. #include <boost/array.hpp>
  14. #include <boost/asio.hpp>
  15. using boost::asio::ip::udp;
  16. int main(int argc,
    char* argv[])
  17. {
  18.   try
  19.    {
  20.     if (argc != 2)
  21.      {
  22.        std::cerr << "Usage: client <host>" << std::endl;
  23.       return 1;
  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_t len = 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. }

抱歉!评论已关闭.