如何通过Qt 5进行LAN唤醒?

时间:2015-03-19 10:44:27

标签: c++ qt qt5 packets wake-on-lan

我发现this tutorial用于LAN唤醒,但我完全无法理解。它在Qt3中实现。我想要可以使用Qt5编译的LAN唤醒功能。如何使用上述代码使用Qt5通过LAN唤醒计算机?

1 个答案:

答案 0 :(得分:5)

这是一个使用Qt 5编译的lan唤醒实现。该函数将目标计算机的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);
}