Sunday, April 02, 2006 3:25 PM bart

Wake on LAN in C#

Some years ago I created a WOL client for Visual Basic 6 using WinSock stuff which was a pretty nice project that wasn't so straightforward to do. Today, I'm creating a little lightweight management tool to apply configuration changes to computers in a network over night, even when the machines are asleep. Shutting down a machine after maintenance is pretty simple to do using the ExitWindowsEx API, but waking a machine up requires some WOL functionality. So I decided to recreate the application (which I couldn't find anymore in my messy non-existing archive ;-)) in managed code (.NET Framework 2.0). More information about WOL can be found on Wikipedia.

Here's the code:

using System;
using
System.Net;
using
System.Net.Sockets;

namespace
WakeOnLan
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] mac = new byte[] {0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35};
            WakeUp(mac);
        }

        /// <summary>
        /// Sends a Wake-On-Lan packet to the specified MAC address.
        /// </summary>
        /// <param name="mac">Physical MAC address to send WOL packet to.</param>
        private static void WakeUp(byte[] mac)
        {
            //
            // WOL packet is sent over UDP 255.255.255.0:40000.
            //
            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 40000);

            //
            // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
            //
            byte[] packet = new byte[17 * 6];

            //
            // Trailer of 6 times 0xFF.
            //
            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            //
            // Body of magic packet contains 16 times the MAC address.
            //
            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            //
            // Submit WOL packet.
            //
            client.Send(packet, packet.Length);
        }
    }
}
Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# B# .NET Blog : Wake on LAN in C#

Sunday, April 09, 2006 1:42 AM by Rickey Whitworth

# Wake on LAN in C#

Monday, April 10, 2006 4:35 PM by Rickey Whitworth

# Nice initative to find code snippets - CodeKeep

Tuesday, April 11, 2006 11:28 PM by B# .NET Blog

Today I ran across CodeKeep, an online repository with tons of code snippets. Actually I was reviewing...

# Nice initative to find code snippets - CodeKeep

Tuesday, April 11, 2006 11:32 PM by B# .NET Blog

Today I ran across CodeKeep, an online repository with tons of code snippets. Actually I was reviewing...

# Hacking HP printers and waking up computers over LAN

Friday, May 05, 2006 12:44 AM by Bite my bytes

# Obtain a computer's MAC address in C# using WMI

Friday, August 18, 2006 7:39 PM by B# .NET Blog

Some time ago I wrote about a C# application to perform Wake-on-LAN. Of course there's one additional...

# Wake on LAN with Linksys WRT54G

Thursday, March 11, 2010 4:28 PM by Krueger Web Design Blog

Wake on LAN with Linksys WRT54G

# Wake on LAN with Linksys WRT54G | Krueger Web Design Blog

Monday, December 20, 2010 8:51 PM by Wake on LAN with Linksys WRT54G | Krueger Web Design Blog

Pingback from  Wake on LAN with Linksys WRT54G | Krueger Web Design Blog

# Accendere computer da remoto in c# &laquo; Micste&#039;s Blog

Tuesday, October 25, 2011 4:40 AM by Accendere computer da remoto in c# « Micste's Blog

Pingback from  Accendere computer da remoto in c# &laquo; Micste&#039;s Blog

# WOL | Pearltrees

Thursday, November 07, 2013 3:46 AM by WOL | Pearltrees

Pingback from  WOL | Pearltrees

# Wake on LAN using C# | Ask Programming &amp; Technology

Sunday, November 17, 2013 7:57 AM by Wake on LAN using C# | Ask Programming & Technology

Pingback from  Wake on LAN using C# | Ask Programming &amp; Technology