进入BIOS一般会发现有网卡唤醒、PCI调制解调器唤醒、串口Ring唤醒和时钟唤醒。一般用户的定时开机需求由时钟唤醒即可解决,不过若是想要在外地也可以轻松打开自己的电脑,网卡唤醒可以解决这个问题。

  网卡唤醒只需要两个参数:广播地址和MAC地址。如果是内网网卡唤醒则只需要MAC地址,广播地址是255.255.255.255。但是怎么知道外网ip的广播地址呢,广播地址等于子网按位求反和IP地址的或运算。

网卡唤醒电脑
public static string GetBroadcast(IPAddress ipAddress, IPAddress subnetMask)
        {
            byte[] ip = ipAddress.GetAddressBytes();
            byte[] sub = subnetMask.GetAddressBytes();

            for (int i = 0; i < ip.Length; i++)
            {
                ip[i] = (byte) ((~sub[i]) | ip[i]); //广播地址=子网按位求反 或 IP地址
            }

            return new IPAddress(ip).ToString();
        }
网卡唤醒电脑

  至此就只需要知道发什么给需要被唤醒的电脑了,MAC魔术封包可以实现网卡唤醒

网卡唤醒电脑
/// <summary>
        ///     字符串转16进制字节数组
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static byte[] StrToHexByte(string hexStr)
        {
            hexStr = hexStr.Replace(" ", "").Replace("-", "").Replace(":", "");
            if ((hexStr.Length%2) != 0)
                hexStr += " ";
            byte[] returnBytes = new byte[hexStr.Length/2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexStr.Substring(i*2, 2), 16);
            return returnBytes;
        }

        /// <summary>
        ///     拼装MAC魔术封包
        /// </summary>
        /// <param name="macStr"></param>
        /// <returns></returns>
        public static byte[] GetMagicPacket(string macStr)
        {
            byte[] returnBytes = new byte[102];
            const string commandStr = "FFFFFFFFFFFF";
            for (int i = 0; i < 6; i++)
                returnBytes[i] = Convert.ToByte(commandStr.Substring(i*2, 2), 16);
            byte[] macBytes = StrToHexByte(macStr);
            for (int i = 6; i < 102; i++)
            {
                returnBytes[i] = macBytes[i%6];
            }
            return returnBytes;
        }
网卡唤醒电脑

相关文章:

  • 2021-07-09
  • 2022-01-15
  • 2022-12-23
  • 2021-09-12
  • 2021-09-15
  • 2021-04-22
  • 2021-11-17
猜你喜欢
  • 2021-06-07
  • 2021-04-07
  • 2021-10-23
  • 2021-06-06
  • 2021-10-22
  • 2021-12-10
  • 2022-12-23
相关资源
相似解决方案