2015-03-19 66 views

回答

4

這裏是局域網喚醒的實現,其使用Qt 5.功能獲取目標計算機的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); 
} 
+0

非常感謝你,Nejat 。 – flybird 2015-03-20 04:42:40

+0

如果我想用我的電腦關閉局域網中的很多電腦,如何用qt5來實現它?謝謝。 – flybird 2015-03-20 09:02:26

+0

我不明白你的意思。我認爲最好在一個新問題中提問並描述你想要的完整內容。 – Nejat 2015-03-20 09:06:00