网卡唤醒电脑

  进入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();
        }
View Code

  至此就只需要知道发什么给需要被唤醒的电脑了,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;
        }
View Code

  另外附上IP转MAC的方法,不过需要在该电脑开启的情况下才能获取它的MAC地址。

  此网卡唤醒做了一个Winform界面的程序,一个控制台的程序,控制台程序由bat文件批量唤醒电脑

@echo off
start D:\NetworkCardWake.exe 00-E0-4C-68-08-E7 183.233.129.53 183.233.129.1
View Code

  本程序有参考其他人的博客,完整程序

转载于:https://www.cnblogs.com/hambert/p/3296920.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值