1、禁用不需要的網卡,比如禁用虛擬機網卡。
2、向所有網卡廣播數據
/* * 直接調用 QUdpSocket 的 writeDatagram() 函數發送數據,如果有多張網卡(裝了虛擬機會增加網卡), * 可能會導致數據發送不出去,原因(猜測):多張網卡意味着多個廣播地址,writeDatagram() 可能只往 * 某個廣播地址發,而這個廣播地址又不是你要發送的廣播地址。。。。。 * 所以:向所有廣播地址廣播數據,保證能向想要廣播地址廣播數據 */ void UdpCommunication::sendUdpData(QByteArray data) { QList<QNetworkInterface> interfaceList = QNetworkInterface::allInterfaces(); foreach (QNetworkInterface interface, interfaceList) { // qDebug() << interface.humanReadableName(); //打印網卡名稱 QList<QNetworkAddressEntry> entryList = interface.addressEntries(); foreach(QNetworkAddressEntry entry, entryList) { QString str = entry.broadcast().toString(); if(str != "") { // qDebug() << str; //打印可用的廣播地址 writeDatagram(data, data.size(), QHostAddress(str), 12345); } } } }