Monday, February 23, 2009

Wake-on-LAN (WoL)

Well, I love the easy development of embedded devices with the .NET Micro Framework. As the device I'm currently using is really small I tried to build a small Web site on it to start my private servers at home using Wake-on-LAN. You'll find a lot of helper methods samples on the Internet but I couldn't find on source code that was working on the .NET Micro Framework. The problem is that there are too many missing methods or enum values.

Here is the source code I'm using now which is working.

public static void WakeUp(byte[] mac) {
    using (Socket socket = new Socket(AddressFamily.InterNetwork,
                                                                           SocketType.Dgram, ProtocolType.Udp))
    {
        IPEndPoint endPoint = new IPEndPoint(new IPAddress(4294967295), 40000);
        // IP 255.255.255.255

        socket.Connect(endPoint);

        byte[] packet = new byte[17 * 6];
        for (int i = 0; i < 6; i++) packet[i] = 0xFF;
        for (int i = 1; i <= 16; i++) {
            for (int j = 0; j < 6; j++) {
                packet[i * 6 + j] = mac[j];
            }
        }

        socket.Send(packet);
    }
}

My network related library for .NET Micro Framework (as well as .NET Compact and the desktop/server version) includes this method.

No comments: