LANDrop发现原理

LANDrop 的发现代码在 discorverservice.cpp 里,仔细研究单独写一个程序。

quint16 PORT_DISCOVERY = 52637;
QUdpSocket udpSocket;

{
    if (!udpSocket.bind(QHostAddress::Any, PORT_DISCOVERY)) {
        QMessageBox::warning(this, QApplication::applicationName(), tr("Unable to bind to port %1.\nYour device won't be discoverable.").arg(PORT_DISCOVERY));
    }
}

void MainWindow::sendMessage()
{
    QString s = ui->lineEdit->text();    
    if (s != "") {
        foreach (const QHostAddress &HA, broadcastAddresses()) {
            udpSocket.writeDatagram(s.toUtf8(), HA, PORT_DISCOVERY);
        }
        ui->textBrowser->append(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss") + "\n" + s);
        ui->lineEdit->setText("");
    }
}

QList MainWindow::broadcastAddresses()
{
    QList list_HA;
    list_HA.append(QHostAddress::Broadcast);
    foreach (const QNetworkInterface &NI, QNetworkInterface::allInterfaces()) {
        if (NI.flags() & QNetworkInterface::CanBroadcast) {
            foreach (const QNetworkAddressEntry &NAE, NI.addressEntries()) {
                list_HA.append(NAE.broadcast());
            }
        }
    }
    qDebug() << list_HA;
    return list_HA;
}

广播地址为什么只有3个,还有一个全255,一个空的?

(QHostAddress("255.255.255.255"), QHostAddress("192.168.2.255"), QHostAddress(""))

发送以下信息即可被发现:

{"device_name":"QT-UDP-HTY","device_type":"Deepin","port":37850,"request":false}

LANDrop发现原理_第1张图片

 

LANDrop发现原理_第2张图片

那么,投屏的发现又是怎样实现的呢?

你可能感兴趣的:(Qt)