using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using Common; using System.Timers; using CommonEntity; namespace ConsoleApplication4 { internal class UdpState { public UdpClient UdpClient { get; set;} public IPEndPoint RemoteEndPoint { get; set; } public UdpState(UdpClient client) { this.UdpClient = client; } } class Program { private static bool _isRunning = true; public static System.Timers.Timer CPUTimer = null; public static void StartCPUTongJiTask() { try { CPUTimer = new System.Timers.Timer(); CPUTimer.Elapsed += new ElapsedEventHandler(CPUtimer_Elapsed); //10分钟统计一次 //现在修改成20秒: //CPUTimer.Interval = 1 * 60 * 1000; CPUTimer.Interval = 1000; CPUTimer.Start(); } catch (Exception ex) { } } static private void CPUtimer_Elapsed(object sender, ElapsedEventArgs e) { var T = sender as System.Timers.Timer; try { // 防止重入 T.Stop(); double d = CPUData.GetCPU(); DataTongJi.CPU_Data.Add(d); Console.WriteLine("aaaaaaaaaa"); } catch (Exception ex) { Console.WriteLine("出错了:"+ex.Message); } finally { // 恢复计时器 T.Start(); } } static void Main(string[] args) { StartCPUTongJiTask(); var a= CPUData.GetCPU(); Console.WriteLine(a); var b = CPUData.GetNowPrecise(); Console.WriteLine(b.ToString()); Console.ReadLine(); //Console.CancelKeyPress += (sender, e) => //{ // _isRunning = false; // e.Cancel = true; //}; //try //{ // var udpClient = new UdpClient(3340); // udpClient.Client.ReceiveBufferSize = 3 * 1024 * 1024; // // 开始接收 // udpClient.BeginReceive(ReceiveCallback, new UdpState(udpClient)); // Console.WriteLine("UDP服务器已启动,按Ctrl+C停止..."); // // 保持程序运行 // while (_isRunning) // { // Thread.Sleep(100); // } // udpClient.Close(); // Console.WriteLine("服务器已停止"); //} //catch (Exception ex) //{ // Console.WriteLine($"启动失败: {ex.Message}"); //} } public static void ReceiveCallback(IAsyncResult ar) { UdpState state = ar.AsyncState as UdpState; try { // 1. 先获取接收到的数据 IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); byte[] receivedData = state.UdpClient.EndReceive(ar, ref remoteEndPoint); // 2. 立即开始下一次接收(不等待数据处理完成) state.UdpClient.BeginReceive(ReceiveCallback, state); // 3. 异步处理数据,避免阻塞接收 ThreadPool.QueueUserWorkItem(_ => { ProcessData(receivedData, remoteEndPoint, state.UdpClient); }); } catch (ObjectDisposedException) { // 正常关闭,忽略 } catch (SocketException ex) { //Console.WriteLine($"网络错误: {ex.SocketErrorCode} - {ex.Message}"); //// 尝试重新开始接收 //if (_isRunning && state?.UdpClient?.Client != null) //{ // try // { // state.UdpClient.BeginReceive(ReceiveCallback, state); // } // catch { } //} } catch (Exception ex) { //Console.WriteLine($"接收回调错误: {ex.Message}"); } } private static void ProcessData(byte[] data, IPEndPoint remoteEP, UdpClient udpClient) { try { //// 这里是您的数据处理逻辑 //Console.WriteLine($"收到来自 {remoteEP} 的数据,长度: {data.Length} 字节"); //// 示例:解码为字符串 //if (data.Length > 0) //{ // string text = Encoding.UTF8.GetString(data); // Console.WriteLine($"内容: {text}"); //} //// 这里可以处理复杂的业务逻辑 //// 例如:数据库操作、文件处理、复杂计算等 //// 如果需要回复 //if (data.Length > 0) //{ // byte[] response = Encoding.UTF8.GetBytes($"已收到: {data.Length} 字节"); // udpClient.Send(response, response.Length, remoteEP); //} } catch (Exception ex) { //Console.WriteLine($"数据处理失败: {ex.Message}"); } } } }