Boost_udp错误

2017.7.28 补充:

1、在IMU程序当中,IMU作为服务器端,向外广播。我们可以在特定的端口上面读到数据。

2、但是,我一直没搞明白就是 udp::sock 的作用,他是可以作为 初始化 赋值来使用的。

     也就是说,他可以作为 一个变量来使用。

     如下:

     

      udp::socket* UDPServer::psocket = NULL;  

      UDPServer::UDPServer(boost::asio::io_service &ios, int port) :  

      socket(ios, udp::endpoint(udp::v4(), port)) {  

      psocket = &socket;  

      listen();  

      } 

   参考资料: http://blog.csdn.net/liujiayu2/article/details/50895384  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

注意一点:当我们不同PC机间进行通信的时候,IP和端口号是不一样的。之前遇到的问题是,boost_system_error,这是因为我们在写程序的时候,发送和接收绑定了同一个端口,导致程序出错。

而且,CANET支持组播通信,也就是说,一个通道可以同时向多个端口发送数据。

之前一直搞错的原因就是端口重复绑定了,导致错误。

  1. /*
  2. * Copyright (c) 2015,北京智行者科技有限公司
  3. * All rights reserved.
  4. *
  5. * 文件名称:Boost_UDP.h
  6. * 文件标识:见软件框架协议
  7. * 摘 要:UDP读写类
  8. *
  9. * 当前版本:1.0
  10. * 作 者:zhuxuekui
  11. * 完成日期:2015年11月30日
  12. * 修 改:
  13. *
  14. * 取代版本:1.0
  15. * 原作者 :zhuxuekui
  16. * 完成日期:2015年11月30日
  17. */
  18.  
  19. #ifndef BOOST_UDP_H
  20. #define BOOST_UDP_H
  21.  
  22. #include "Utils.h"
  23.  
  24. using namespace boost;
  25.  
  26. #define RECVSIZE 1024
  27. class Boost_UDP
  28. {
  29. public:
  30.  
  31.    Boost_UDP(boost::asio::io_service &io_service,string pcIP, int pcPort, string canetIP, int canetPort):udp_sock(io_service)
  32.    {
  33.        m_canetIP = canetIP;
  34.       m_canetPort = canetPort;
  35.       m_pcIP = pcIP;
  36.       m_pcPort = pcPort;
  37.    }
  38.  
  39.    ~Boost_UDP()
  40.    {
  41.       udp_sock.close();
  42.    }
  43.  
  44.    //开始socket,绑定端口等。 绑定PC机端口号,而且还不能用 127.0.0.1, 不然会出错,很奇怪的原因。
  45.    void start_sock()
  46.    {
  47.       // here the ip can change to 192.168.1.33
  48.        boost::asio::ip::udp::endpoint local_add(boost::asio::ip::address_v4::from_string(m_pcIP),m_pcPort);
  49.       udp_sock.open(local_add.protocol());
  50.       udp_sock.bind(local_add);
  51.    }
  52.  
  53.    //收数据
  54.    int receive_data(unsigned char buf[])
  55.    {
  56.       boost::mutex::scoped_lock lock(mutex);
  57.        //donot change 目的端口与发送端口现在是不一样的
  58.        boost::asio::ip::udp::endpoint send_endpoint(boost::asio::ip::address_v4::from_string(m_pcIP),m_pcPort); //这里的endpoint是PC机的IP和端口号
  59.        int ret = udp_sock.receive_from(boost::asio::buffer(buf,RECVSIZE),send_endpoint);//堵塞模式
  60.       return ret;
  61.    }
  62.  
  63.    //发送数据
  64.    int send_data(unsigned char str[], int len)
  65.    {
  66.       boost::mutex::scoped_lock lock(mutex);
  67.       //donot change
  68.       boost::asio::ip::udp::endpoint send_endpoint(boost::asio::ip::address_v4::from_string(m_canetIP),m_canetPort); //canet的IP和端口号
  69.       int ret = udp_sock.send_to(boost::asio::buffer(str,len),send_endpoint);
  70.       return ret;
  71.    }
  72. public:
  73.  
  74.    string m_canetIP;
  75.    int m_canetPort;
  76.    string m_pcIP;
  77.    int m_pcPort;
  78.  
  79.    boost::asio::ip::udp::socket udp_sock;
  80.    mutable boost::mutex mutex;
  81.  
  82. };
  83.  
  84.  
  85. #endif
原文地址:https://www.cnblogs.com/zhuxuekui/p/5219842.html