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库加入组播的代码

c 复制代码
#include <iostream>
#include <boost/asio.hpp>

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;
};

  • Boost库报错信息时No such device

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

  • 设置默认路由即可解决程序崩溃问题
shell 复制代码
route add -net 0.0.0.0 netmask 0.0.0.0 dev enp0s3
  • 查看路由表

|------------------------------------------|
| Linux系统中必须设置0.0.0.0的默认路由规则,否则程序加入组播时会宕掉。 |

相关推荐
tobebetter95274 小时前
How to manage python versions on windows
开发语言·windows·python
9***P3344 小时前
PHP代码覆盖率
开发语言·php·代码覆盖率
CoderYanger5 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
jllllyuz5 小时前
Matlab实现基于Matrix Pencil算法实现声源信号角度和时间估计
开发语言·算法·matlab
多多*5 小时前
Java复习 操作系统原理 计算机网络相关 2025年11月23日
java·开发语言·网络·算法·spring·microsoft·maven
凌康ACG5 小时前
Sciter之c++与前端交互(五)
c++·sciter
p***43485 小时前
Rust网络编程模型
开发语言·网络·rust
ᐇ9595 小时前
Java集合框架深度实战:构建智能教育管理与娱乐系统
java·开发语言·娱乐
梁正雄6 小时前
1、python基础语法
开发语言·python
强化学习与机器人控制仿真6 小时前
RSL-RL:开源人形机器人强化学习控制研究库
开发语言·人工智能·stm32·神经网络·机器人·强化学习·模仿学习