修改 在线离线的判断逻辑
This commit is contained in:
@@ -1,50 +1,130 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Net.Sockets;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace ConsoleApplication4
|
||||
{
|
||||
internal class UdpState
|
||||
{
|
||||
private UdpClient udpClient;
|
||||
public UdpClient UdpClient { get; set;}
|
||||
public IPEndPoint RemoteEndPoint { get; set; }
|
||||
|
||||
public UdpState(UdpClient client)
|
||||
{
|
||||
this.udpClient = client;
|
||||
}
|
||||
public UdpClient UdpClient
|
||||
{
|
||||
get { return this.udpClient; }
|
||||
this.UdpClient = client;
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
private static bool _isRunning = true;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var udpClient = new UdpClient(3340);
|
||||
udpClient.Client.ReceiveBufferSize = 3 * 1024 * 1024;
|
||||
udpClient.BeginReceive(ReceiveCallback, new UdpState(udpClient));
|
||||
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;
|
||||
// 1. 结束异步接收,获取数据和远程端点
|
||||
IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
|
||||
byte[] receivedData = state.UdpClient.EndReceive(ar, ref remoteEndPoint);
|
||||
|
||||
state.UdpClient.BeginReceive(ReceiveCallback, state);
|
||||
|
||||
try
|
||||
{
|
||||
Console.WriteLine(11111111111111);
|
||||
// 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 (Exception)
|
||||
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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user