当另一个进程已经绑定(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/