1using System;
2using System.Net;
3using System.Net.Sockets;
4
5namespace WakeOnLan
6{
7    class Program
8    {
9        static void Main(string[] args)
10        {
11            byte[] mac = new byte[] {0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35};
12            WakeUp(mac);
13        }
14
15        /// <summary>
16        /// Sends a Wake-On-Lan packet to the specified MAC address.
17        /// </summary>
18        /// <param name="mac">Physical MAC address to send WOL packet to.</param>
19        private static void WakeUp(byte[] mac)
20        {
21            //
22            // WOL packet is sent over UDP 255.255.255.0:40000.
23            //
24            UdpClient client = new UdpClient();
25            client.Connect(IPAddress.Broadcast, 40000);
26
27            //
28            // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
29            //
30            byte[] packet = new byte[17 * 6];
31
32            //
33            // Trailer of 6 times 0xFF.
34            //
35            for (int i = 0; i < 6; i++)
36                packet[i] = 0xFF;
37
38            //
39            // Body of magic packet contains 16 times the MAC address.
40            //
41            for (int i = 1; i <= 16; i++)
42                for (int j = 0; j < 6; j++)
43                    packet[i * 6 + j] = mac[j];
44
45            //
46            // Submit WOL packet.
47            //
48            client.Send(packet, packet.Length);
49        }
50    }
51}
From  http://www.codekeep.net/snippets/57f1c93c-ecde-4f3a-889c-03ca674c17f7.aspx

創作者介紹

PowerShell@HungYuWu

ayowu 發表在 痞客邦 PIXNET 留言(0) 人氣()