Search code examples
c++qtqt5packetswake-on-lan

How to do Wake On LAN by Qt 5?


I found this tutorial for wake on LAN,but I can not understand it completely. It is implemented in Qt3. I want the wake on LAN functionality which could be compiled with Qt5. How can I use the mentioned code to wake a computer through LAN using Qt5?


Solution

  • Here is an implementation for wake on lan which compiles with Qt 5. The function takes the MAC address of the destination computer as an argument and broadcasts the relevant UDP packet :

    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);
    }