Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
            {
                var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
                var ipConfig = "127.0.0.1";
                var portConfig = 53470;
                var toIp = "255.255.255.255";
                var toPort = 53490;
                if (config != null)
                {
                    if (!string.IsNullOrWhiteSpace(config.sendIp))
                    {
                        ipConfig = config.sendIp;
                    }
                    if (!string.IsNullOrWhiteSpace(config.sendPort))
                    {
                        int.TryParse(config.sendPort, out portConfig);
                    }
                    if (!string.IsNullOrWhiteSpace(config.toIp))
                    {
                        toIp = config.toIp;
                    }
                    if (!string.IsNullOrWhiteSpace(config.toPort))
                    {
                        int.TryParse(config.toPort, out toPort);
                    }
                }
                else 
                {
                    ipConfig = "127.0.0.1";
                }
                localIpep = new IPEndPoint(IPAddress.Parse(ipConfig), portConfig); // 本机IP和监听端口号
                
                udpcSend = new UdpClient(localIpep);
 
                byte[] sendbytes = data;
                IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(toIp), toPort); // 发送到的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 { }
        }
    }
}