www.pudn.com > WOL.zip > WOLClass.cs


using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Globalization; 
using System.Net; 
using System.Net.Sockets; 
 
namespace WakeOnLan 
{ 
    class WOLClass:UdpClient 
    { 
        public WOLClass(): base() 
        { } 
        //this is needed to send broadcast packet 
        private void SetClientToBrodcastMode() 
        { 
            if (this.Active) 
                this.Client.SetSocketOption(SocketOptionLevel.Socket, 
                                          SocketOptionName.Broadcast, 0); 
        } 
 
        public void WakeFunction(string MAC_ADDRESS) 
        { 
            Connect(new IPAddress(0xffffffff),  //255.255.255.255  i.e broadcast 
               0x2fff); // port=12287 
            SetClientToBrodcastMode(); 
            //set sending bites 
            int counter = 0; 
 
            //用于唤醒的数据包 
            byte[] bytes = new byte[1024]; 
 
            //前6个字节使用0xFF填充 
            for (int y = 0; y < 6; y++) 
                bytes[counter++] = 0xFF; 
 
            //MAC地址重复16次 
            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; 
                } 
            } 
 
            //发送唤醒数据包 
            int reterned_value = Send(bytes, 1024); 
        } 
    } 
}