Sending a wake on LAN packet from c# can actually be very easy.

Using the following class it make it even easier. All you need to know is the
mac address of the machine you are attempt to wakeup.

public static void WakeUp(string MAC_ADDRESS, IPAddress IPBCast)
{
    UdpClient UDP = new UdpClient();

    try
    {
        UDP.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);

        int offset = 0;
        byte[] buffer = new byte[512];   // more than enough :-)

        //first 6 bytes should be 0xFF
        for (int y = 0; y < 6; y++)
            buffer[offset++] = 0xFF;

            //now repeate MAC 16 times
            for (int y = 0; y < 16; y++)
            {
                int i = 0;
                for (int z = 0; z < 6; z++)
                {
                    buffer[offset++] =
                        byte.Parse(MAC_ADDRESS.Substring(i, 2), NumberStyles.HexNumber);
                    i += 2;
                }
            }

            UDP.EnableBroadcast = true;
            UDP.Send(buffer, 512, new IPEndPoint(IPBCast, 0x1));
        }
        catch (Exception ex)
        {
            UDP.Close();
            throw (ex);
        }
    }
}

All you need to do is call the function with a valid MAC address and a broadcast ip address. In most cases 255.255.255.255 will work. However if you want to broadcast across a certain remote network on another subnet you may need to user X.X.255.255 for example. Otherwise the packet will not be routed to the correct network.



Did You find this page useful?

Yes No



Last Modified: 23 February 2017

Releated Posts


2012-08-09 - C++ - Check an IP Address is in a IP Mask
2012-03-08 - C - UDP Socket example
2012-02-17 - C - IP address validation
2012-02-15 - C - Get current ip address of an interface
2011-12-11 - C - Linux get mac address from interface
2011-10-27 - CSharp - Number Of Cores and Processors
2011-10-04 - CSharp - Thread Queue Example
2011-09-29 - CSharp - Background Thread / Task
2011-09-17 - CSharp - Lookup Hostname
2011-08-29 - CSharp - IsGuid
2011-08-29 - C Sharp - StopWatch, The high resolution timer
2011-08-26 - CSharp - Convert Between Meters and Feet
2011-08-23 - CSharp - ThreadPool and Windows Forms
2011-08-10 - C Sharp - MD5Sum / SHASum
2011-07-22 - CSharp - Fibonacci sum
2011-07-13 - CSharp - Palindrome
2011-07-11 - CSharp - The string Reverse
2011-07-10 - CSharp - Interviews FizzBuzz
2011-06-27 - CSharp - HowTo Parse a URL
2011-06-22 - CSharp - Extending any class
2011-06-13 - ASPNET - Login by username or email
2011-05-12 - CSharp - ASPNET Logoff
2011-03-07 - C Sharp - Windows Logoff
2011-03-02 - ASPNET - Dynamic Controls
2011-03-01 - C Sharp - Resize an image by size of height
2011-02-23 - C Sharp / MSSQL Get inserted value of NEWSEQUENTIALID()
2011-02-10 - CSharp - Wake on Lan (WOL) Packet
2011-02-10 - CSharp - Shutdown or reboot windows
2011-02-06 - C Sharp - Gridview Bound Yes No
2011-02-05 - MSSQL - Levenshtein
2011-01-30 - MSSQL - Enabling CLR Functions
2011-01-30 - MSSQL - Adding MD5 checksum support
2011-01-30 - MSSQL - RegEx Support
2011-01-30 - MSSQL - Adding SHASum support