优化Redis 接收逻辑

This commit is contained in:
2026-03-13 11:32:46 +08:00
parent 03a27dbde8
commit da7356f016
18 changed files with 254 additions and 17 deletions

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace ConsoleApplication4
{
internal class UdpState
{
private UdpClient udpClient;
public UdpState(UdpClient client)
{
this.udpClient = client;
}
public UdpClient UdpClient
{
get { return this.udpClient; }
}
}
class Program
{
static void Main(string[] args)
{
var udpClient = new UdpClient(3340);
udpClient.Client.ReceiveBufferSize = 3 * 1024 * 1024;
udpClient.BeginReceive(ReceiveCallback, new UdpState(udpClient));
Console.ReadLine();
}
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);
}
catch (Exception)
{
}
}
}
}