4

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?

Nejat
  • 28,909
  • 10
  • 91
  • 119
flybird
  • 53
  • 6

1 Answers1

5

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);
}
Nejat
  • 28,909
  • 10
  • 91
  • 119
  • if i want to use my computer to close many computers in LAN,how to achieve it with the qt5? thanks. – flybird Mar 20 '15 at 09:02
  • I can't get what you mean exactly. I think it's better to ask it in a new question and describe what you want completely. – Nejat Mar 20 '15 at 09:06
  • I don't know any way to do that. You should probably configure them not to ask for a password. – Nejat Mar 24 '15 at 07:39