如何通过Qt 5实现局域网唤醒



我在局域网上找到了这个唤醒教程,但我不能完全理解。它在Qt3中实现。我想要可以用Qt5编译的局域网唤醒功能。如何使用Qt5通过局域网使用上述代码唤醒计算机?

这里是用Qt5编译的局域网唤醒的实现。该函数以目标计算机的MAC地址为自变量,并广播相关的UDP数据包:

void MyClass::wakeOnLan(QString MAC)
{
    char MACAddr [6];
    char MagicPacket [102]; // Magic package for remote boot
    int j = sscanf (MAC.toLatin1().data(), "%2x-%2x-%2x-%2x-%2x-%2x",
                    & MACAddr [0], & MACAddr [1], & MACAddr [2], & MACAddr [3], & MACAddr [4], & MACAddr [5]);
    // Set to hexadecimal before the magicpacket array 6 characters ff
    memset (MagicPacket, 0xff, 6);
    int packetsize = 6; // the beginning of the initial value is 6, do not wrong. I is because the effect of the initial value of the written as 0, it is too effortless.
    // Build MagicPacket.
    for (int i = 0; i <16; i++)
    {
        memcpy (MagicPacket + packetsize, MACAddr, 6);
        packetsize += 6;
    }
    QHostAddress FakeAddress;
    FakeAddress.setAddress ("192.168.0.255");
    QUdpSocket udpSocket;
    udpSocket.writeDatagram(MagicPacket, 102, FakeAddress, 9);
}

相关内容

  • 没有找到相关文章