• C++使用Boost库加入UDP组播时程序崩溃


    程序崩溃情况

    • 本程序运行在Oracle VM VirtualBox虚拟的Ubuntu20.04

      terminate called after throwing an instance of ‘boost::wrapexceptboost::system::system_error’ what(): set_option: No such device 已放弃 (核心已转储) **

    • C++使用Boost库加入组播的代码

    #include 
    #include 
    
    class MulticastReceiver
    {
    public:
        MulticastReceiver(boost::asio::io_context &ioContext, const std::string &multicastAddress, const std::string &listenAddress, int port)
            : ioContext(ioContext),
              socket(ioContext),
              multicastEndpoint(boost::asio::ip::address::from_string(multicastAddress), port),
              listenEndpoint(boost::asio::ip::address::from_string(listenAddress), port)
        {
            // 创建套接字
            socket.open(listenEndpoint.protocol());
    
            // 设置套接字选项,允许地址重用
            socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
    
            // 绑定到监听地址
            socket.bind(listenEndpoint);
    
            // 加入组播组
            socket.set_option(boost::asio::ip::multicast::join_group(multicastEndpoint.address()));
        }
    
        void start()
        {
            receive();
        }
    
    private:
        void receive()
        {
            socket.async_receive_from(boost::asio::buffer(buffer), senderEndpoint,
                                      [this](const boost::system::error_code &error, std::size_t bytesTransferred)
                                      {
                                          if (!error)
                                          {
                                              // 处理接收到的数据
                                              std::cout << "Received: " << std::string(buffer.data(), bytesTransferred) << std::endl;
    
                                              // 继续接收数据
                                              receive();
                                          }
                                          else
                                          {
                                              std::cerr << "Error receiving data: " << error.message() << std::endl;
                                          }
                                      });
        }
    
    private:
        boost::asio::io_context &ioContext;
        boost::asio::ip::udp::socket socket;
        boost::asio::ip::udp::endpoint multicastEndpoint;
        boost::asio::ip::udp::endpoint listenEndpoint;
        std::array<char, 1024> buffer;
        boost::asio::ip::udp::endpoint senderEndpoint;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    • Boost库报错信息时No such device

    Boost库中boost::asio::ip::udp::socket套接字找不到网卡去加入组播。

    • 设置默认路由即可解决程序崩溃问题
    route add -net 0.0.0.0 netmask 0.0.0.0 dev enp0s3
    
    • 1
    • 查看路由表
      在这里插入图片描述
    Linux系统中必须设置0.0.0.0的默认路由规则,否则程序加入组播时会宕掉。
  • 相关阅读:
    【JS】对象字面量 代替 switch 方法
    VLAN trunk扩展 MUXVLAN 原理与实验
    Vue中动态绑定class和style
    JVM第七讲:JVM 基础 - Java 内存模型详解
    C++11新特性之十六:std::tie
    【PCBA方案设计】蓝牙跳绳方案
    国产FPGA高云GW1NSR-4C,集成ARM Cortex-M3硬核
    数据分析 | Pandas 200道练习题 进阶篇(2)
    Unity 宣布正式收购 Weta Digital
    02 【JS表达式与操作符】
  • 原文地址:https://blog.csdn.net/Love_XiaoQinEr/article/details/132907492