Wake On Lan Packet (C#)
The following code will send out a WOL packet to a server specified by the MAC Address. Please note this only works for boards that have a WOL option in the CMOS.
using System;
using System.Globalization;
using System.Net.Sockets;
using System.Net;
//we derive our class from a standard 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);
}
public static void Main()
{
WakeFunction("00018054D467");
}
public static void WakeFunction(string MAC_ADDRESS)
{
//now use this class
//MAC_ADDRESS should look like '013FA049'
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 bytes
int counter=0;
//buffer to be send
byte[] bytes=new byte[1024];
//first 6 bytes should be 0xFF
for(int y=0;y<6;y++) style="color:#33cc00;">
//now repeate MAC 16 times
for(int y=0;y<16;y++) i="0;" z="0;z<6;z++)" style="color:#33cc00;">
//now send wake up packet
int reterned_value=client.Send(bytes,1024);
}
}
using System;
using System.Globalization;
using System.Net.Sockets;
using System.Net;
//we derive our class from a standard 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);
}
public static void Main()
{
WakeFunction("00018054D467");
}
public static void WakeFunction(string MAC_ADDRESS)
{
//now use this class
//MAC_ADDRESS should look like '013FA049'
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 bytes
int counter=0;
//buffer to be send
byte[] bytes=new byte[1024];
//first 6 bytes should be 0xFF
for(int y=0;y<6;y++) style="color:#33cc00;">
//now repeate MAC 16 times
for(int y=0;y<16;y++) i="0;" z="0;z<6;z++)" style="color:#33cc00;">
//now send wake up packet
int reterned_value=client.Send(bytes,1024);
}
}
0 Comments:
Post a Comment
<< Home