InPowerS.Net

 找回密碼
 註冊
搜索
查看: 2529|回復: 0

C# Wake On LAN (WOL)

[複製鏈接]
發表於 2009-3-3 02:04:38 | 顯示全部樓層 |閱讀模式

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;

  4. namespace WakeOnLan
  5. {
  6.     class Program
  7.     {
  8.      static void Main(string[] args)
  9.      {
  10.      byte[] mac = new byte[] {0x00, 0x0F, 0x1F, 0x20, 0x2D, 0x35};
  11.      WakeUp(mac);
  12.      }

  13.      /// <summary>
  14.      /// Sends a Wake-On-Lan packet to the specified MAC address.
  15.      /// </summary>
  16.      /// <param name="mac">Physical MAC address to send WOL packet to.</param>
  17.      private static void WakeUp(byte[] mac)
  18.      {
  19.      //
  20.      // WOL packet is sent over UDP 255.255.255.0:40000.
  21.      //
  22.      UdpClient client = new UdpClient();
  23.      client.Connect(IPAddress.Broadcast, 40000);

  24.      //
  25.      // WOL packet contains a 6-bytes trailer and 16 times a 6-bytes sequence containing the MAC address.
  26.      //
  27.      byte[] packet = new byte[17 * 6];

  28.      //
  29.      // Trailer of 6 times 0xFF.
  30.      //
  31.      for (int i = 0; i < 6; i++)
  32.      packet = 0xFF;

  33.      //
  34.      // Body of magic packet contains 16 times the MAC address.
  35.      //
  36.      for (int i = 1; i <= 16; i++)
  37.      for (int j = 0; j < 6; j++)
  38.      packet[i * 6 + j] = mac[j];

  39.      //
  40.      // Submit WOL packet.
  41.      //
  42.      client.Send(packet, packet.Length);
  43.      }
  44.     }
  45. }
複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

小黑屋|Archiver|手機版|InPowerS.Net

GMT+8, 2018-10-22 01:22

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表