QT UDP多网卡广播 亲测可用

          QList networkinterfaces = QNetworkInterface::allInterfaces();
          foreach (QNetworkInterface interfaces, networkinterfaces) {
              foreach (QNetworkAddressEntry entry, interfaces.addressEntries())
              {
                QHostAddress broadcastAddress = entry.broadcast();
                if (broadcastAddress != QHostAddress::Null
                      && entry.ip() != QHostAddress::LocalHost
                      && entry.ip().protocol() == QAbstractSocket::IPv4Protocol
                    )
                {
                    QJsonObject jsonObjectip,jsonObject;
                    jsonObjectip.insert("IP", (entry.ip()).toString());
                    jsonObject.insert("content", jsonObjectip);
                    jsonObject.insert("Title","notify_door_gateway");

                    QJsonDocument jsonDocument;
                    jsonDocument.setObject(jsonObject);
                    QByteArray dataArray = jsonDocument.toJson();
                    m_udpSocket->writeDatagram(dataArray,broadcastAddress, 8082);
                }

              }

          }

 

你可能感兴趣的:(QT UDP多网卡广播 亲测可用)