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的默认路由规则,否则程序加入组播时会宕掉。 |

相关推荐
翻滚丷大头鱼12 分钟前
Java 集合Collection—List
java·开发语言
小欣加油13 分钟前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
王璐WL24 分钟前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae31 分钟前
C++ -- 模板
开发语言·c++·笔记·其他
胡耀超31 分钟前
4、Python面向对象编程与模块化设计
开发语言·python·ai·大模型·conda·anaconda
索迪迈科技1 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法
慢慢沉2 小时前
UDP与TCP的区别
网络协议·tcp/ip·udp
Zz_waiting.2 小时前
Javaweb - 14.6 - Vue3 数据交互 Axios
开发语言·前端·javascript·vue·axios
萌新小码农‍2 小时前
Java分页 Element—UI
java·开发语言·ui
Tiger_shl2 小时前
【.Net技术栈梳理】03-核心框架与运行时(异常处理)
开发语言·.net