Files
Web_CRICS_Server_VS2010_Prod/ConsoleApplication4/Program.cs

179 lines
5.7 KiB
C#
Raw Normal View History

2026-03-13 11:32:46 +08:00
using System;
using System.Net;
2026-03-16 13:57:23 +08:00
using System.Net.Sockets;
using System.Text;
using System.Threading;
2026-03-25 17:51:43 +08:00
using Common;
using System.Timers;
using CommonEntity;
2026-03-13 11:32:46 +08:00
namespace ConsoleApplication4
{
internal class UdpState
{
2026-03-16 13:57:23 +08:00
public UdpClient UdpClient { get; set;}
public IPEndPoint RemoteEndPoint { get; set; }
2026-03-13 11:32:46 +08:00
public UdpState(UdpClient client)
{
2026-03-16 13:57:23 +08:00
this.UdpClient = client;
2026-03-13 11:32:46 +08:00
}
}
2026-03-16 13:57:23 +08:00
2026-03-13 11:32:46 +08:00
class Program
{
2026-03-16 13:57:23 +08:00
private static bool _isRunning = true;
2026-03-25 17:51:43 +08:00
public static System.Timers.Timer CPUTimer = null;
public static void StartCPUTongJiTask()
2026-03-13 11:32:46 +08:00
{
2026-03-25 17:51:43 +08:00
try
2026-03-16 13:57:23 +08:00
{
2026-03-25 17:51:43 +08:00
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;
2026-03-16 13:57:23 +08:00
try
{
2026-03-25 17:51:43 +08:00
// 防止重入
T.Stop();
double d = CPUData.GetCPU();
DataTongJi.CPU_Data.Add(d);
Console.WriteLine("aaaaaaaaaa");
2026-03-16 13:57:23 +08:00
}
catch (Exception ex)
{
2026-03-25 17:51:43 +08:00
Console.WriteLine("出错了:"+ex.Message);
2026-03-16 13:57:23 +08:00
}
2026-03-25 17:51:43 +08:00
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}");
//}
2026-03-13 11:32:46 +08:00
}
public static void ReceiveCallback(IAsyncResult ar)
{
UdpState state = ar.AsyncState as UdpState;
2026-03-16 13:57:23 +08:00
2026-03-13 11:32:46 +08:00
try
{
2026-03-16 13:57:23 +08:00
// 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);
});
2026-03-13 11:32:46 +08:00
}
2026-03-16 13:57:23 +08:00
catch (ObjectDisposedException)
2026-03-13 11:32:46 +08:00
{
2026-03-16 13:57:23 +08:00
// 正常关闭,忽略
}
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}");
2026-03-13 11:32:46 +08:00
}
}
}
2026-03-16 13:57:23 +08:00
}