优化Redis 接收逻辑
This commit is contained in:
50
ConsoleApplication4/Program.cs
Normal file
50
ConsoleApplication4/Program.cs
Normal 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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user