Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#
Article

Wake On Lan sample for C#

Rate me:
Please Sign up or sign in to vote.
4.22/5 (23 votes)
27 Oct 2003 230.4K   88   19
Wake On Lan utility using C#.

Introduction

It's strange but I failed to find on the Internet any WOL sample written in C#. So I spent some time making a program to that purpose. It's very simple but it works OK (in our intranet at least).

For waking computers, I use Magic Packet (MAC address of network adapter repeated 16 times).

The actual functionality is implemented like this:

Code

C#
using System;
using System.Net.Sockets;

//we derive our class from a standart one
public class WOLClass:UdpClient    
{
    public WOLClass():base()
    { }
    //this is needed to send broadcast packet
    public void SetClientToBrodcastMode()    
    {
      if(this.Active)
       this.Client.SetSocketOption(SocketOptionLevel.Socket,
                                 SocketOptionName.Broadcast,0);
    }
 }
 
...
//now use this class
 //MAC_ADDRESS should  look like '013FA049'
 private void WakeFunction(string MAC_ADDRESS)   
 {
      WOLClass client=new WOLClass();
      client.Connect(new 
         IPAddress(0xffffffff),  //255.255.255.255  i.e broadcast
         0x2fff); // port=12287 let's use this one 
      client.SetClientToBrodcastMode();
      //set sending bites
      int counter=0;
      //buffer to be send
      byte[] bytes=new byte[1024];   // more than enough :-)
     //first 6 bytes should be 0xFF
     for(int y=0;y<6;y++)
        bytes[counter++]=0xFF;
     //now repeate MAC 16 times
     for(int y=0;y<16;y++)
     {
         int i=0;
         for(int z=0;z<6;z++)
         {
              bytes[counter++]= 
                  byte.Parse(MAC_ADDRESS.Substring(i,2),
                  NumberStyles.HexNumber);
              i+=2;
         }
     }

     //now send wake up packet
     int reterned_value=client.Send(bytes,1024);
 }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Satellite Provider Satellite Provider
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionShutdown On Lan Pin
Member 150805569-May-21 23:48
Member 150805569-May-21 23:48 
QuestionNote - you need to adjust Network Adapter power settings Pin
navyjax218-Apr-17 9:21
navyjax218-Apr-17 9:21 
PraiseWorks just fine Pin
Jefferson Motta6-Apr-17 5:33
Jefferson Motta6-Apr-17 5:33 
QuestionReturn values from WakeFunction ? Pin
KeldSo7-Feb-16 4:47
KeldSo7-Feb-16 4:47 
GeneralWorks. Pin
Member 154943823-Sep-14 22:57
Member 154943823-Sep-14 22:57 
QuestionNot working on my system.. Pin
Basmeh Awad12-May-13 22:58
professionalBasmeh Awad12-May-13 22:58 
AnswerRe: Not working on my system.. Pin
Jefferson Motta7-Aug-17 5:37
Jefferson Motta7-Aug-17 5:37 
GeneralWorks with WOW Pin
David J Rundle15-May-12 4:34
David J Rundle15-May-12 4:34 
GeneralThanks! Pin
Anthraxx NL22-Jan-10 10:33
Anthraxx NL22-Jan-10 10:33 
GeneralOther example and some theory Pin
memos-software24-Jun-08 20:31
memos-software24-Jun-08 20:31 
GeneralError Pin
Christian Rasmussen11-Jun-08 3:11
Christian Rasmussen11-Jun-08 3:11 
GeneralRe: Error Pin
Ian_E15-Jun-08 12:35
Ian_E15-Jun-08 12:35 
You are using conflicting terminology in your query. An "external machine within the network"... Which is it?

Same Subnet
If you mean a different machine on the same subnet (i.e. behind the same router):

1. Verify that the MAC address you have is correct.

2. Verify that the machine you're trying to wake supports Wake on LAN ("WOL") functionality:

2A. BIOS Settings
Go into the machine's BIOS and look for a setting that enables WOL.

2B. Is Your Power Supply Compatible?
Note that your machine's power supply must also support this capability; if your power supply does not have its own "master" on/off switch, it's likely that it's not compatible. Modern power supplies supply a trickle voltage to the computer's motherboard (in "active off" mode); this is routed to the Ethernet adapter, among other things.

2C. Is Wake On LAN Configured in Windows (Windows XP assumed below)
a) In Windows, go into Control Panel & bring up Network Connections.
b) Right-click the Network Adapter corresponding to the MAC address you're trying to wake, & click "Properties".
c) In the Properties dialog, click the "Configure..." button (next to the adapter name).
d) In the "Connection Properties" dialog, look for a Power Management (or similarly named) tab. (This may or may not be there and the name will vary, depending on the manufacturer of your Ethernet adapter & the driver you have installed.)
e) If you do have a Power Management tab, look for options that enable/disable any of the following:

- "Wake on Directed Packet"
- "Wake on Magic Packet"
- "Wake on Magic Packet from power off state"
- "Wake on Link"

These are what I have for an Intel PRO/1000 Ethernet adapter on my machine. You would need to enable the 2 Wake on Magic Packet options (I leave the other 2 disabled).

2D. Note also that for certain add-in Ethernet adapters (PCI) and mother boards you may need to connect a 3-wire "WOL Cable" (from header on card to header on motherboard) for the WOL Capability to work. You would generally find this on really old cards & motherboards (more than 5 years old).

Finally, a note about states:
S1 = On (Operational)
S3 = Standby (enable "Wake on Magic Packet" to allow wakeup from this state)
S5 = Active Off (enable "Wake on Magic Packet from power off state" to allow wakeup from this state)

Different Subnet
If you mean a machine on a different subnet and your magic packet has to be passed outside your local router, I doubt that this is possible. I have read elsewhere that routers do not forward broadcast packets. If it's a router that you control in your own domain, you *may* be able to configure it to do so - someone else would need to comment on this possibility.
GeneralRe: Error Pin
Randy_Miller12-Jul-08 6:14
Randy_Miller12-Jul-08 6:14 
GeneralThanks! Pin
devcow11-Jun-07 7:16
devcow11-Jun-07 7:16 
GeneralWorks great ! Pin
A.J.Bauer11-Mar-07 6:32
A.J.Bauer11-Mar-07 6:32 
Generalfor broadcasting Pin
plato2926-Jun-06 22:16
plato2926-Jun-06 22:16 
Generalbroadcasting Pin
plato2926-Jun-06 21:42
plato2926-Jun-06 21:42 
GeneralIP Broadcasting Pin
HakunaMatada27-Dec-05 22:29
HakunaMatada27-Dec-05 22:29 
GeneralThanks for the sample Pin
Radoslav Bielik6-Jan-04 11:05
Radoslav Bielik6-Jan-04 11:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.