[英]UDP Client after several send/receives stops receiving and blocks port
我正在嘗試使用UWP向UDP多播地址發送和從中接收。 它在最初的幾次中運行良好,但是經過一段時間的發送-接收過程后,它將鎖定接收部分。 我從異步方法更改為同步方法,但仍然相同。 即使我實例化一個新的UDP客戶端,該端口也會被阻塞,直到重新啟動該應用程序為止。 我做錯了什么嗎?
private UdpClient udp;
//inside main function:
if (udp == null)
{
udp = new UdpClient(new IPEndPoint(IPAddress.Any, portNumber));
//^the second time this is called, it will complain about port reuse
udp.Client.ReceiveTimeout = udp.Client.SendTimeout = 3000;
//udp.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.ReuseAddress, true);
//^invalid
}
//await udp.SendAsync(data, data.Length, , portNumber);
//I changed from async to synchronous in case it was the issue, but no.
udp.Client.SendTo(data, new IPEndPoint(IPAddress.Parse(ipString), portNumber));
//the receive used to be async, too
byte[] receivedByte = new byte[udp.Client.ReceiveBufferSize];
try
{
udp.Client.Receive(receivedByte);
}
catch (Exception ex)
{
udp.Client.Shutdown(SocketShutdown.Both);
udp = null; // added these, but port still blocked until restart
}
我正在使用UWP,並且類庫中的方法不在這里。
在將UdpClient放入using()語句而不是將其聲明為私有字段,並通過將其放入簡短的異步方法來限制其范圍之后,我不再遇到這些問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.