WOL远程开机

  最近在一直都在研究PC机硬件和软件相结合的软件,硬件信息都是通过C++与驱动结合获取。对于一个好久都没有接触C++的人来说看这些东西太费劲了,必须的重新捡一下C++的基础知识,必然也少不了C知识,底层都是通过C++与C结合,提供接口给J2EE调用,J2EE也忘的一干二净了。从C++那也了解到了AMT、ACPI、DPM等不少驱动的结合,可以取到哪些硬件信息和对硬件操作,有空就使用C#做了Demo,不过还是使用C#比较得心应手。

  这次是试验了一下网卡的Wake On Lan功能,就是能够在广域网和局域网能远程启动目标机器,需要网卡支持Wake on Lan功能,关闭机器后网卡的灯会一直亮着,还需要检查以下2点设置。

  1. 进入BIOS设置,Power->Automatic Power On里面,设置Wake on LAN = Enable/Automatic,不同机器的BIOS设置位置不同,找到对应的Wake on LAN选项设置就OK。

  2. 进入网卡设置,我的电脑->右键”管理“->设备管理器->网络适配器,找到对应的网卡右键”属性“->电源管理,勾选允许此设备唤醒计算机和子选项(只允许幻数据包唤醒计算机),”高级“选项卡里面,检查属性里的唤醒幻数据包=已启用 and 唤醒模式匹配=已启用。

  注:不同的网卡设置可能会不一样

  下面就用代码详细说明实现方式:

#region WOL远程唤醒机器
        /// <summary>
        /// 通过WOL远程唤醒机器方法
        /// 摘要:唤醒方法为网卡提供的魔术封包功能,即以广播模式发送6个FF加上16遍目标机器MAC地址的字节数组
        /// </summary>
        /// <param name="mac">要唤醒机器的MAC</param>
        /// <param name="ip">要唤醒机器的子网掩码</param>
        /// <param name="port">UDP消息发送端口</param>
        private static void WakeOnLan(string mac,string ip, int port)
        {
            IPEndPoint point;
            UdpClient client = new UdpClient();
            byte[] magicBytes = GetMagicPacket(mac);
            point = new IPEndPoint(IPAddress.Parse(ip), port);//广播模式:255.255.255.255
            try
            {
                int result = client.Send(magicBytes, magicBytes.Length, point);
            }
            catch (SocketException ex) {
                throw new Exception(ex.ToString());
            }
        }        
        /// <summary>
        /// 拼装MAC魔术封包
        /// </summary>
        /// <param name="hexString">MAC地址字符串</param>
        /// <returns></returns>
        public static byte[] GetMagicPacket(string macString)
        {
            byte[] returnBytes = new byte[102];
            string commandString = "FFFFFFFFFFFF";
            for (int i = 0; i < 6; i++)
                returnBytes[i] = Convert.ToByte(commandString.Substring(i * 2, 2), 16);
            byte[] macBytes = StrToHexByte(macString);
            for (int i = 6; i < 102; i++)
            {
                returnBytes[i] = macBytes[i % 6];
            }
            return returnBytes;
        }
        /// <summary>
        /// MAC地址字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString">MAC地址字符串</param>
        /// <returns></returns>
        public static byte[] StrToHexByte(string hexString)
        {
            hexString = hexString.Replace("-", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
        #endregion

   调用主函数:

static void Main(string[] args)
        {
            string macAddress, ipAddress;
            int port;
            Console.Write("Please input MAC:");
            macAddress = Console.ReadLine();
            if (string.IsNullOrEmpty(macAddress))
            {
                macAddress = "F8-0F-41-21-12-68";
                Console.WriteLine("Default MAC:" + macAddress);
            }

            Console.Write("Please input Subnet:");
            ipAddress = Console.ReadLine();
            if (string.IsNullOrEmpty(ipAddress))
            {
                ipAddress = "255.255.255.255";
                Console.WriteLine("Default Subnet:" + ipAddress);
            }

            Console.Write("Please input Port:");
            if (!string.IsNullOrEmpty(Console.ReadLine()))
            {
                port = int.Parse(Console.ReadLine());
            }
            else
            {
                port = 9000;
                Console.WriteLine("Default Port:" + port);
            }

            WakeOnLan(macAddress, ipAddress, port);

            Console.Read();
        }

   

  这个实例就介绍到这里吧,欢迎大家来拍砖!如有其它好的方法希望不要吝啬,拿出来分享给大家一块学习进步!

原文地址:https://www.cnblogs.com/ZHF/p/3303082.html