c#网络唤醒局域网内计算机(用mac地址)

方法一:

 public void WakeUp(string macString)
    {
        try
        {
            if (macString.Split('-').Length == 6)
            {
                string macStr = macString.Replace('-', ' '); ;

                byte[] macByteArray = HexStringSToByteArray(macStr);

                UdpClient client = new UdpClient();
                client.Connect(IPAddress.Broadcast, 1234);//IPAddress.Broadcast:默认广播地址"255.255.255.255"
                //client.Connect("127.0.0.1", 1234);

                //print(IPAddress.Broadcast);

                byte[] packet = new byte[17 * 6];
                for (int i = 0; i < 6; i++)
                {
                    packet[i] = 0xFF;
                }

                for (int i = 1; i <= 16; i++)
                {
                    for (int ii = 0; ii < 6; ii++)
                    {
                        packet[i * 6 + ii] = macByteArray[ii];
                    }
                }

                int a = client.Send(packet, packet.Length);
                print("开机指令发送");                
            }
        }
        catch (Exception ex)
        {
            // MessageBox.Show("网络唤醒失败:" + ex.StackTrace + " \r\n" + ex.Message);
        }


    }
    /// <summary>
    /// 2位16进制转int
    /// </summary>
    /// <param name="hex"></param>
    /// <returns></returns>
    public static int HexStringToInt(string hex)
    {
        int num1 = 0;
        int num2 = 0;
        char[] nums = hex.ToCharArray();
        if (hex.Length == 2)
        {
            for (int i = 0; i < nums.Length; i++)
            {
                String strNum = nums[i].ToString().ToUpper();
                switch (strNum)
                {
                    case "A":
                        strNum = "10";
                        break;
                    case "B":
                        strNum = "11";
                        break;
                    case "C":
                        strNum = "12";
                        break;
                    case "D":
                        strNum = "13";
                        break;
                    case "E":
                        strNum = "14";
                        break;
                    case "F":
                        strNum = "15";
                        break;
                    default:

                        break;
                }
                if (i == 0)
                {
                    num1 = int.Parse(strNum) * 16;
                }
                if (i == 1)
                {
                    num2 = int.Parse(strNum);
                }
            }
        }

        return num1 + num2;
    }
    /// <summary>
    /// 16进制字符串转byte[] 48 65 6C 6C 6F 20 57 6F 72 6C 64 21
    /// </summary>
    /// <param name="hexValues"></param>
    /// <returns></returns>
    public static byte[] HexStringSToByteArray(string hexValues)
    {

        string[] hexValuesSplit = hexValues.Split(' ');
        byte[] val = new byte[hexValuesSplit.Length];
        int i = 0;
        foreach (string hex in hexValuesSplit)
        {
            int value = HexStringToInt(hex);
            val[i] = (byte)value;
            i++;
        }

        return val;
    }

原文地址:
https://blog.csdn.net/taoerit/article/details/83538863?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf

方法二:

private IPEndPoint point;
private UdpClient client = new UdpClient();
/**
 * 唤醒远程机器方法
 * @param
 * mac 要唤醒的机器的MAC
 * IP
 * port udp消息发送端口
 *
 * 摘要:唤醒方法为网卡提供的魔术封包功能,即以广播模式发送6个FF加上16遍目标MAC地址的字节数组
 **/
private void wakeUp(string mac, int port, string ip)
{
    byte[] magicBytes = getMagicPacket(mac);//例:"18 31 BF 4D DC 9A"
    point = new IPEndPoint(IPAddress.Parse(ip), port);//广播模式:255.255.255.255
    try
    {
        client.Send(magicBytes, magicBytes.Length, point);
    }
    catch (SocketException e) { MessageBox.Show(e.Message); }
}

/// <summary>
/// 字符串转16进制字节数组
/// </summary>
/// <param name="hexString"></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;
}

/// <summary>
/// 拼装MAC魔术封包
/// </summary>
/// <param name="hexString"></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;
}

原文地址:https://www.xp.cn/b.php/112825.html

方法三:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Net;
namespace 远程唤醒开机
{
    ///
    /// MainWindow.xaml 的交互逻辑
    ///
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static void WakeUp(byte[] mac)
        {
            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 9090);
            //MessageBox.Show(IPAddress.Broadcast.ToString());
            byte[] packet = new byte[17 * 6];
            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;
            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];
            int result = client.Send(packet, packet.Length);
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        byte[] mac = new byte[6];
        mac[0] = 0x00;
        mac[1] = 0x0B;
        mac[2] = 0x2F;
        mac[3] = 0x70;
        mac[4] = 0x40;
        mac[5] = 0x9E;
        WakeUp(mac);
    }
    }
}

原文链接:https://blog.csdn.net/wangshubo1989/article/details/48286383
方法四:

/// <summary>
        /// 远程开机,网卡需要具备远程唤醒功能
        /// </summary>
        /// <param name="mac">网卡物理地址字符数组</param>
        public static void WakeUp(byte[] mac)
        {
            UdpClient client = new UdpClient();
            client.Connect(IPAddress.Broadcast, 9090);

            byte[] packet = new byte[17 * 6];

            for (int i = 0; i < 6; i++)
                packet[i] = 0xFF;

            for (int i = 1; i <= 16; i++)
                for (int j = 0; j < 6; j++)
                    packet[i * 6 + j] = mac[j];

            int result = client.Send(packet, packet.Length);
        }

        // 唤醒按钮事件代码

        private void button_Click(object sender, EventArgs e)
        {
            byte[] mac = new byte[6];
            mac[0] = 0x00;
            mac[1] = 0x01;
            mac[2] = 0x80;
            mac[3] = 0x79;
            mac[4] = 0x08;
            mac[5] = 0xD8;

            WakeUp(mac);
        }

以上几种写法逻辑都大同小异,都是通过广播ip发送给对应mac地址的设备一个包含一串"F"的信号,要注意的前提是,需要被网络唤醒的电脑一定要打开bois设置里的"允许网络唤醒"选项

完整流程可参考方法一的原文:
https://blog.csdn.net/taoerit/article/details/83538863?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.add_param_isCf

posted @ 2022-06-22 18:05  哒哒哒~~~  阅读(440)  评论(0编辑  收藏  举报