C# 關閉UDP時如何處理?
我在點擊一個按鈕時,會彈出一個窗體Form2,
private void Form2_Load(object sender, EventArgs e)
{
GlobalUDP.UDPClient = new UdpClient();
GlobalUDP.EP = new IPEndPoint(IPAddress.Parse("255.255.255.255"), 26868);
IPEndPoint BindEP = new IPEndPoint(IPAddress.Any, 26868);
byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery:Who is out there?");
GlobalUDP.UDPClient.Client.Bind(BindEP);
GlobalUDP.UDPClient.MulticastLoopback = false;
GlobalUDP.UDPClient.EnableBroadcast = true;
//Configure ourself to receive discovery responses
GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP);
// Transmit the discovery request message
GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
}
當關閉Form2窗體,並再次點開Form2窗體時,會出現如下錯誤:
Only one usage of each socket address (protocol/network address/port) is normally permitted
請教:應該在關閉窗體時如何處理?
------解决方案--------------------
关注,UP上去
------解决方案--------------------
在你关闭窗体时要把资源释放完