Moli0
2021-12-15 24d68e7998e80cbd4ef3ed04acbf2539a9c5a46b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
 
namespace WOLAwaken.Helper
{
    public class SendUdpPack
    {
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        UdpClient udpcSend = null;
 
        IPEndPoint localIpep = null;
 
        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="obj"></param>
        public void SendMessage(string obj)
        {
            try
            {
                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 本机IP和监听端口号
                udpcSend = new UdpClient(localIpep);
 
                string message = obj;
                byte[] sendbytes = Encoding.Unicode.GetBytes(message);
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 发送到的IP地址和端口号
                udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
                //udpcSend.Close();
            }
            catch { }
        }
 
        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="obj"></param>
        public void SendMessage(byte[] data)
        {
            try
            {
                localIpep = new IPEndPoint(IPAddress.Parse("192.168.2.111"), 53470); // 本机IP和监听端口号
                udpcSend = new UdpClient(localIpep);
 
                byte[] sendbytes = data;
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 53490); // 发送到的IP地址和端口号
                string str = "";
                foreach (var a in sendbytes)
                {
                    str += $"|{Convert.ToInt32(a)}";
                }
                Console.WriteLine($"即将发送开机包:{Convert.ToString(str)}");
                udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
                Console.WriteLine($"已发送:{str}");
                udpcSend.Close();
            }
            catch { }
        }
    }
}