c++ - 如何通过 Qt 5 进行局域网唤醒?

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

我找到了 this tutorial for wake on LAN,但我不能完全理解。它在 Qt3 中实现。我想要可以用 Qt5 编译的局域网唤醒功能。如何使用上述代码使用 Qt5 通过 LAN 唤醒计算机?

最佳答案

这是一个用 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);
}

关于c++ - 如何通过 Qt 5 进行局域网唤醒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142457/

相关文章:

c++ - 将数组作为函数参数传递,并在其上调用开始/结束方法

python - C++ (STL map) 等效于 Python 的 setdefault 的方法

c++ - 关于google-nacl的非常简单的问题

c++ - Qt:更新 QString 时自动生成的代码失败

qt - 当用户打开菜单或调整窗口大小时,QMainWindow 停止接收 QEvent::UpdateRequest

c++ - 使用 NEW 而不是 MALLOC 的 GetAdapterAddresses 函数

android -/usr/lib/x86_64-linux-gnu/libQt5Core.so.5 : version `Qt_5.7' not found (required by emulator64-x86)

qt - QML ItemDelegate 突出显示的属性不起作用

c++ - 服务器需要 cookie 时的 QT HTTP 发布问题

qt - 将 QML 的 ShaderEffect 剪辑为圆形