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
96
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 ReceiveUDPPack
    {
 
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        static UdpClient udpcRecv = null;
 
        static IPEndPoint localIpep = null;
 
        /// <summary>
        /// 开关:在监听UDP报文阶段为true,否则为false
        /// </summary>
        static bool IsUdpcRecvStart = false;
        /// <summary>
        /// 线程:不断监听UDP报文
        /// </summary>
        static Task thrRecv;
 
        public static void StartReceive()
        {
            var receiveIp = "127.0.0.1";
            var receivePort = 53490;
            var config = Helpers.Tool.GetSystemConfig().baseSysConfg;
            if (config != null) 
            {
                if (!string.IsNullOrWhiteSpace(config.receiveIp))
                {
                    receiveIp = config.receiveIp;
                }
                if (!string.IsNullOrWhiteSpace(config.receivePort))
                {
                    int.TryParse(config.toPort, out receivePort);
                }
            }
            if (!IsUdpcRecvStart) // 未监听的情况,开始监听
            {
                localIpep = new IPEndPoint(IPAddress.Parse(receiveIp), receivePort); // 本机IP和监听端口号
                udpcRecv = new UdpClient(localIpep);
                thrRecv = new Task(() => { ReceiveMessage(); });
                thrRecv.Start();
                IsUdpcRecvStart = true;
                Console.WriteLine("UDP监听器已成功启动");
            }
        }
 
        public static void StopReceive()
        {
            if (IsUdpcRecvStart)
            {
                thrRecv.Dispose(); // 必须先关闭这个线程,否则会异常
                udpcRecv.Close();
                IsUdpcRecvStart = false;
                Console.WriteLine("UDP监听器已成功关闭");
            }
        }
 
        /// <summary>
        /// 接收数据
        /// </summary>
        /// <param name="obj"></param>
        private static void ReceiveMessage()
        {
            while (IsUdpcRecvStart)
            {
                try
                {
                    byte[] bytRecv = udpcRecv.Receive(ref localIpep);
                    foreach (var a in bytRecv)
                    {
                        Console.Write(a);
                        Console.Write("-");
                    }
                    string message = Encoding.UTF8.GetString(bytRecv, 0, bytRecv.Length);
                    Console.WriteLine(string.Format("{0}[{1}]", localIpep, message));
 
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
        }
    }
}