几次 send/receives 后 UDP 客户端停止接收并阻塞端口
UDP Client after several send/receives stops receiving and blocks port
我正在尝试使用 UWP 发送和接收 to/from 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,class 库中有一些方法不在此处。
将 UdpClient 放在 using () 语句中而不是将其声明为私有字段,并通过将其放在简短的异步方法中来限制其范围后,我不再遇到这些问题了。
我正在尝试使用 UWP 发送和接收 to/from 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,class 库中有一些方法不在此处。
将 UdpClient 放在 using () 语句中而不是将其声明为私有字段,并通过将其放在简短的异步方法中来限制其范围后,我不再遇到这些问题了。