使用ShareAddress的多播的QUdpSocket绑定失败 [英] QUdpSocket bind for multicast with ShareAddress fails

查看:4956
本文介绍了使用ShareAddress的多播的QUdpSocket绑定失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在另一个进程已经绑定到指定端口上的情况下,下面的简单代码在带有QT5.2的Windows上失败。然而,ShareAddress标志应该允许绑定多次。在MacOSX它工作正常。

  if(false == bind(QHostAddress :: AnyIPv4,port_,QUdpSocket :: ShareAddress) 
{
qDebug()<< Warn:不能绑定到多播端口<港口_;
qDebug()<<错误();
}
joinMulticastGroup(QHostAddress(address_));

这是QT中的错误吗?

解决方案

如果是的话,在文档中说明,在Windows平台上忽略ShareAddress


允许其他服务绑定到同一个地址和端口。当多个进程通过侦听相同的地址和端口(例如,具有多个预先分配的侦听器的web服务器可以大大改善响应时间)来共享单个服务的负载时,这是有用的。但是,由于允许重新绑定任何服务,此选项受到某些安全注意事项的约束。请注意,通过将此选项与ReuseAddressHint组合,您还将允许您的服务重新绑定现有的共享地址。在Unix上,这相当于SO_REUSEADDR套接字选项。在Windows上,此选项被忽略。


我从来没有测试过这个解决方案, $ c> QUdpSocket :: ReuseAddressHint 作为Windows的替代标志。


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.

解决方案

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.

这篇关于使用ShareAddress的多播的QUdpSocket绑定失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆