C# UDPClient仅接收一个数据报
我正在尝试读取连续的UDP字节(或者一次至少读取1个以上)。我的缓冲区是单个数据报/数据包大小的4倍(1000字节)C# UDPClient仅接收一个数据报,c#,udp,C#,Udp,我正在尝试读取连续的UDP字节(或者一次至少读取1个以上)。我的缓冲区是单个数据报/数据包大小的4倍(1000字节) 一次只读取一个数据报数据包。如果你想读取多个数据报,你需要多次调用接收< /代码>。 如果你想接收多个数据包,你应该考虑使用异步方法来接收它们。UdpClient类有一个异步习惯用法,您可以给它一个回调函数,然后继续执行其他处理(例如更新UI) 你要发送多少数据?你们寄的速度是多少? Byte[] receiveBytes = new Byte[4096]; UdpCli
一次只读取一个数据报数据包。如果你想读取多个数据报,你需要多次调用<代码>接收< /代码>。 如果你想接收多个数据包,你应该考虑使用异步方法来接收它们。UdpClient类有一个异步习惯用法,您可以给它一个回调函数,然后继续执行其他处理(例如更新UI)
你要发送多少数据?你们寄的速度是多少?
Byte[] receiveBytes = new Byte[4096];
UdpClient udpClient = new UdpClient(56885);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
public static bool messageReceived = false;
public static void ReceiveCallback(IAsyncResult ar)
{
UdpClient u = (UdpClient)((UdpState)(ar.AsyncState)).u;
IPEndPoint e = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
Byte[] receiveBytes = u.EndReceive(ar, ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("Received: {0}", receiveString);
messageReceived = true;
}
public static void ReceiveMessages()
{
// Receive a message and write it to the console.
IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(e);
UdpState s = new UdpState();
s.e = e;
s.u = u;
Console.WriteLine("listening for messages");
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
// Do some work while we wait for a message. For this example,
// we'll just sleep
while (!messageReceived)
{
Thread.Sleep(100);
}
}