c++ - QUdpSocket 与 ShareAddress 的多播绑定(bind)失败

标签 c++ qt udp

当另一个进程已经绑定(bind)到指定端口时,下面的简单代码在带有 QT5.2 的 Windows 上失败。然而,ShareAddress 标志应该允许绑定(bind)多次。在 MacOSX 上运行良好。

if (false == bind(QHostAddress::AnyIPv4, port_, QUdpSocket::ShareAddress))
{
    qDebug() << "Warn: cannot bind to the multicast port " << port_;
    qDebug() << error();
}
joinMulticastGroup(QHostAddress(address_));

这是 QT 中的错误吗?如果是,是否有解决方法?

谢谢。

最佳答案

documentation 中所述, ShareAddress 在 Windows 平台上被忽略。

Allow other services to bind to the same address and port. This is useful when multiple processes share the load of a single service by listening to the same address and port (e.g., a web server with several pre-forked listeners can greatly improve response time). However, because any service is allowed to rebind, this option is subject to certain security considerations. Note that by combining this option with ReuseAddressHint, you will also allow your service to rebind an existing shared address. On Unix, this is equivalent to the SO_REUSEADDR socket option. On Windows, this option is ignored.

我从未测试过这个解决方案,但据我了解,您可以尝试使用 QUdpSocket::ReuseAddressHint 作为 Windows 的替代标志。

关于c++ - QUdpSocket 与 ShareAddress 的多播绑定(bind)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25719515/

相关文章:

c++ - 图像顶部的 Qt 显示按钮

c++ - 如何在静态方法中实例化当前类的实例?

c# - .Net套接字(UDP)发送,接收和计划

php - 在 PHP 中获取 UDP 组播流

c++ - 三元运算符中的条件 move 或复制赋值

c++ - 将 DirectShow 过滤器图转储到图像或文本文件

c++ - Qt - QPropertyAnimation 中存在错误?

c++ - 原始套接字的 udp 数据包碎片

c++ - boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它工作正常,为什么?

QT 在 *nix 上打开默认文件资源管理器