QUdpSocket bind for multicast with ShareAddress fa

2019-08-03 22:44发布

The simple code belows fails on Windows with QT5.2 when another process is already bound on the specified port. The ShareAddress flag should allow binding multiple times however. On MacOSX it works fine.

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

Is it a bug in QT ? If so is there a workaround ?

Thanks.

标签: c++ qt udp
1条回答
祖国的老花朵
2楼-- · 2019-08-03 23:19

As stated in the documentation, ShareAddress is ignored on Windows platform.

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.

I never test this solution, but in my understanding, you can try to use QUdpSocket::ReuseAddressHint as alternative flags for Windows.

查看更多
登录 后发表回答