Qt 组播问题 Win10 Win11

文章讲述了在Windows11中,使用Qt进行本地IP绑定和加入多播组的过程比Win10更为简洁,而在Win10中需要设置组播网卡并指定接口。详细比较了两个版本中操作的异同。
摘要由CSDN通过智能技术生成

Win11 中 qt 仅需绑定指定接口的本地ip和加入组就可以了

Win10中 需要先设置组播网卡,然后在加入组,入组时还不能再指定接口。具体原因不详

```

setMulticastInterface(lni[nIndex]);

ret = ret?joinMulticastGroup(m_mhost, lni[nIndex]):ret;

```


bool MMultiCast::bindmcast(const char *maddr, int port, const char* ifip)
{
    bool ret = false;
    QVariant ttl = 10;
    QVariant enable = false;
    strncpy(m_maddr, maddr, sizeof(m_maddr));
    m_mhost = QHostAddress(m_maddr);
    m_port = port;
    const QList<QNetworkInterface> &lni = QNetworkInterface::allInterfaces();
//    ret = bind(QHostAddress::AnyIPv4, port, ShareAddress|ReuseAddressHint);
    ret = bind(ifip?QHostAddress(ifip):QHostAddress::AnyIPv4, port, ShareAddress|ReuseAddressHint);
    int nIndex = -1;
    if (QSysInfo::kernelVersion().startsWith("10.0.2")) {
        ret = joinMulticastGroup(m_mhost);
    } else {
        for (int i = 0; i < lni.size(); i++)
        {
            qDebug() << "setMulticastInterface"<< i << "name" << lni[i].humanReadableName();
            for (auto addr : lni[i].addressEntries())
            {
                if (addr.ip().toString().compare(ifip) == 0) {
                    if (nIndex == -1) {
                        nIndex = i;
                        qDebug() << "setMulticastInterface nIndex" << nIndex << ifip;
                    }
                    qDebug() << "same ip " << nIndex << ifip;
                }
            }
        }
    }
    if (nIndex >= 0) {
        setMulticastInterface(lni[nIndex]);
        ret = ret?joinMulticastGroup(m_mhost, lni[nIndex]):ret;
    }
    if (!ret) {
        qDebug() << "join group " << maddr << " failed!!! bind local ip " << ifip;
        return false;
    }
    qDebug() << "bind " << ifip << ret << localAddress();

    setSocketOption(MulticastTtlOption, ttl);
    setSocketOption(MulticastLoopbackOption, enable);
    connect(this, &QUdpSocket::readyRead, this, &MMultiCast::onReadReady);
    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值