Click here to Skip to main content
Click here to Skip to main content

Wake On Lan sample for C#

By , 27 Oct 2003
 

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

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

About the Author

maxburov
Satellite Provider Satellite Provider
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot working on my system..memberMember 1002944612 May '13 - 22:58 
GeneralWorks with WOWmemberDavid j rundle15 May '12 - 4:34 
GeneralThanks!memberAnthraxx NL22 Jan '10 - 10:33 
GeneralOther example and some theorymembermemos-software24 Jun '08 - 20:31 
GeneralErrormemberChristian Rasmussen11 Jun '08 - 3:11 
GeneralRe: ErrormemberIan_E15 Jun '08 - 12:35 
GeneralRe: ErrormemberRandy_Miller12 Jul '08 - 6:14 
GeneralThanks!memberdevcow11 Jun '07 - 7:16 
GeneralWorks great !memberA.J.Bauer11 Mar '07 - 6:32 
Generalfor broadcastingmemberplato2926 Jun '06 - 22:16 
Generalbroadcastingmemberplato2926 Jun '06 - 21:42 
GeneralIP BroadcastingmemberSave My Soul - (SMS)27 Dec '05 - 22:29 
GeneralThanks for the samplememberRadoslav Bielik6 Jan '04 - 11:05 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 28 Oct 2003
Article Copyright 2003 by maxburov
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid