异步 Udp 消息的缓冲区
Buffer for async Udp Messages
我正在使用以下代码接收消息(我使用这种方式是因为我在等待消息时正在做一些事情)但是,我正在测试这个连接并且我发送的消息彼此非常接近但是好像其中一个迷路了。我不知道如何解决这个问题。如何启用 'read buffer' 选项或类似选项?
谢谢..这是代码:
//接收UDP报文的函数
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 IpEndPoint_groupEP);
received_data_from_client = Encoding.ASCII.GetString(receiveBytes);
messageReceived = true;
Logger("Received a message from : " + IpEndPoint_groupEP.ToString());
u.Close();
}
public static void ReceiveMessages()
{
IpEndPoint_groupEP.Address = (IPAddress.Any);
IpEndPoint_groupEP.Port = listenPort;
//IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(IpEndPoint_groupEP);
UdpState s = new UdpState();
s.e = IpEndPoint_groupEP;
s.u = u;
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
while (!messageReceived)
{
Thread.Sleep(50);
Console.writeLine("Hello")
}
我有 u.close 因为这个函数被调用了不止一个。我有一个 while 循环,因此程序读取消息并对消息执行某些操作,然后在完成后循环并调用 ReceiveMessages 函数开始读取另一条消息。基本上,当程序没有收到消息时,它会大喊 'Hello'
它就像一个魅力。但是我意识到当两条或更多条消息同时到达时,它只读取其中一条而我正在丢失其他消息
问题是您正在关闭消息之间的端口。与其这样做,不如将端口的创建移到 ReceiveMessages()
之外
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 IpEndPoint_groupEP);
received_data_from_client = Encoding.ASCII.GetString(receiveBytes);
messageReceived = true;
Logger("Received a message from : " + IpEndPoint_groupEP.ToString());
}
public static UdpClient CreateClient()
{
IpEndPoint_groupEP.Address = (IPAddress.Any);
IpEndPoint_groupEP.Port = listenPort;
UdpClient u = new UdpClient(IpEndPoint_groupEP);
return u;
}
public static void ReceiveMessages(UdpClient u)
{
UdpState s = new UdpState();
s.e = IpEndPoint_groupEP;
s.u = u;
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
while (!messageReceived)
{
Thread.Sleep(50);
Console.writeLine("Hello")
}
}
然而,因为您在执行 BeginReceive
之后唯一要做的就是开始循环等待什么都不做,您可能需要考虑摆脱异步回调并直接调用 Receive
并且阻止那个。
我正在使用以下代码接收消息(我使用这种方式是因为我在等待消息时正在做一些事情)但是,我正在测试这个连接并且我发送的消息彼此非常接近但是好像其中一个迷路了。我不知道如何解决这个问题。如何启用 'read buffer' 选项或类似选项?
谢谢..这是代码:
//接收UDP报文的函数
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 IpEndPoint_groupEP);
received_data_from_client = Encoding.ASCII.GetString(receiveBytes);
messageReceived = true;
Logger("Received a message from : " + IpEndPoint_groupEP.ToString());
u.Close();
}
public static void ReceiveMessages()
{
IpEndPoint_groupEP.Address = (IPAddress.Any);
IpEndPoint_groupEP.Port = listenPort;
//IPEndPoint e = new IPEndPoint(IPAddress.Any, listenPort);
UdpClient u = new UdpClient(IpEndPoint_groupEP);
UdpState s = new UdpState();
s.e = IpEndPoint_groupEP;
s.u = u;
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
while (!messageReceived)
{
Thread.Sleep(50);
Console.writeLine("Hello")
}
我有 u.close 因为这个函数被调用了不止一个。我有一个 while 循环,因此程序读取消息并对消息执行某些操作,然后在完成后循环并调用 ReceiveMessages 函数开始读取另一条消息。基本上,当程序没有收到消息时,它会大喊 'Hello'
它就像一个魅力。但是我意识到当两条或更多条消息同时到达时,它只读取其中一条而我正在丢失其他消息
问题是您正在关闭消息之间的端口。与其这样做,不如将端口的创建移到 ReceiveMessages()
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 IpEndPoint_groupEP);
received_data_from_client = Encoding.ASCII.GetString(receiveBytes);
messageReceived = true;
Logger("Received a message from : " + IpEndPoint_groupEP.ToString());
}
public static UdpClient CreateClient()
{
IpEndPoint_groupEP.Address = (IPAddress.Any);
IpEndPoint_groupEP.Port = listenPort;
UdpClient u = new UdpClient(IpEndPoint_groupEP);
return u;
}
public static void ReceiveMessages(UdpClient u)
{
UdpState s = new UdpState();
s.e = IpEndPoint_groupEP;
s.u = u;
u.BeginReceive(new AsyncCallback(ReceiveCallback), s);
while (!messageReceived)
{
Thread.Sleep(50);
Console.writeLine("Hello")
}
}
然而,因为您在执行 BeginReceive
之后唯一要做的就是开始循环等待什么都不做,您可能需要考虑摆脱异步回调并直接调用 Receive
并且阻止那个。