如何通过 Qt 5 进行局域网唤醒?
How to do Wake On LAN by Qt 5?
我找到了this tutorial for wake on LAN,但是我完全看不懂。它在 Qt3 中实现。我想要可以用 Qt5 编译的局域网唤醒功能。如何使用上述代码使用 Qt5 通过 LAN 唤醒计算机?
这里是使用 Qt 5 编译的 wake on 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);
}
我找到了this tutorial for wake on LAN,但是我完全看不懂。它在 Qt3 中实现。我想要可以用 Qt5 编译的局域网唤醒功能。如何使用上述代码使用 Qt5 通过 LAN 唤醒计算机?
这里是使用 Qt 5 编译的 wake on 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);
}