Qt-报错 QNativeSocketEngine::joinMulticastGroup() was not called in QAbstractSocket::BoundState
相关资料:
https://blog.csdn.net/xinguhong4645/article/details/99581508
https://blog.csdn.net/wg8826/article/details/80818673
CSDN:编写Qt udpsocket网络编程组播时候提示
:报错 QNativeSocketEngine::joinMulticastGroup() was not called in QAbstractSocket::BoundState
原因:需要将端口号选择可以复用。修改代码如下
udpsocket->bind(QHostAddress::AnyIPv4,8888);
1
改为
//绑定
udpsocket->bind(QHostAddress::AnyIPv4,8888,QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressHint);
1
2
即可。
-------------------------------------------------------------------------
QUdpSocket *revWBudp;
revWBudp = new QUdpSocket;
//绑定本地端口
revWBudp->bind(QHostAddress::AnyIPv4, multicastPort, QUdpSocket::NotShareAddress);
//加入组播组
bool result = revWBudp->joinMulticastGroup(QHostAddress(multicastIP));//这行报错
将
QUdpSocket::NotShareAddress
改为
QUdpSocket::ShareAddress即可
结果如下:
QUdpSocket *revWBudp;
revWBudp = new QUdpSocket;
//绑定本地端口
revWBudp->bind(QHostAddress::AnyIPv4, multicastPort, QUdpSocket::ShareAddress);
//加入组播组
bool result = revWBudp->joinMulticastGroup(QHostAddress(multicastIP));
欢迎关注我,一起进步!扫描下方二维码即可加我