Click here to Skip to main content
6,632,966 members and growing! (19,659 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Network     Intermediate

Wake On Lan sample for C#

By maxburov

Wake On Lan utility using C#.
C#.NET 1.0, Win2K, WinXP, Visual Studio, Dev
Posted:27 Oct 2003
Views:70,397
Bookmarked:60 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.85 Rating: 3.13 out of 5
3 votes, 17.6%
1
2 votes, 11.8%
2
3 votes, 17.6%
3
3 votes, 17.6%
4
6 votes, 35.3%
5

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


Member

Location: Satellite Provider Satellite Provider

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralOther example and some theory Pinmembermemos-software21:31 24 Jun '08  
GeneralError PinmemberChristian Rasmussen4:11 11 Jun '08  
GeneralRe: Error PinmemberIan_E13:35 15 Jun '08  
GeneralRe: Error PinmemberRandy_Miller7:14 12 Jul '08  
GeneralThanks! Pinmemberdevcow8:16 11 Jun '07  
GeneralWorks great ! PinmemberA.J.Bauer7:32 11 Mar '07  
Generalfor broadcasting Pinmemberplato2923:16 26 Jun '06  
Generalbroadcasting Pinmemberplato2922:42 26 Jun '06  
GeneralIP Broadcasting PinmemberSave My Soul - (SMS)23:29 27 Dec '05  
GeneralThanks for the sample PinmemberRadoslav Bielik12:05 6 Jan '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Oct 2003
Editor: Smitha Vijayan
Copyright 2003 by maxburov
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project