0

I can't figure out how to send a WOL (WakeOnLan) using IoT.

It seams like I should se DatagramSocket, but all samples I can find online, uses UDPClient.

How can I send WOL (UDP) in IoT?

Thanks.

MojoDK
  • 4,410
  • 10
  • 42
  • 80

1 Answers1

0

To send a magic packet in a WinRT app you indeed need to use DatagramSocket from the Windows.Networking.Sockets API. This is a basic solution I'd written a while back:

public async void SendMagicPacket(string macAddress, string ipAddress, string port)
{
    DatagramSocket socket = new DatagramSocket();
    await socket.ConnectAsync(new HostName(ipAddress), port);
    DataWriter writer = new DataWriter(socket.OutputStream);

    byte[] datagram = new byte[102];

    for (int i = 0; i <= 5; i++)
    {
        datagram[i] = 0xff;
    }

    string[] macDigits = null;
    if (macAddress.Contains("-"))
    {
        macDigits = macAddress.Split('-');
    }
    else if (macAddress.Contains(":"))
    {
        macDigits = macAddress.Split(':');
    }

    if (macDigits.Length != 6)
    {
        throw new ArgumentException("Incorrect MAC address");
    }

    int start = 6;
    for (int i = 0; i < 16; i++)
    {
        for (int x = 0; x < 6; x++)
        {
            datagram[start + i * 6 + x] = (byte)Convert.ToInt32(macDigits[x], 16);
        }
    }

    writer.WriteBytes(datagram);
    await writer.StoreAsync();
    socket.Dispose();
}
Mentha Suaveolens
  • 261
  • 1
  • 3
  • 11