有Main窗體,上面有個Button,當點擊Button時,有如下代碼:
Form1 frm = new Form1();
            frm.ShowDialog();以下為Form1的部分代碼:
        private void Form1_Load(object sender, EventArgs e)
        {
            GetUDPStatus();
        }
     struct UdpState
        {
            public System.Net.IPEndPoint EP;
            public System.Net.Sockets.UdpClient UDPClient;
        }         private void GetUDPStatus()
        {
            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;            GlobalUDP.UDPClient.BeginReceive(ReceiveCallback, GlobalUDP);
            GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
        }        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchHost();
        }        private void SearchHost()
        {
            listView1.Items.Clear();            byte[] DiscoverMsg = Encoding.ASCII.GetBytes("Discovery: Who is out there?");
            GlobalUDP.UDPClient.Send(DiscoverMsg, DiscoverMsg.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), 26868));
        }        public void AddDiscoveryEntry(object o)
        {
            listView1.Items.Add(new ListViewItem(((string)(o)).Split('\n')));
        }        public void ReceiveCallback(IAsyncResult ar)
        {
            UdpState MyUDP = (UdpState)ar.AsyncState;            string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP));            //ReceiveString = MyUDP.EP.Address.ToString() + "\n" + ReceiveString.Replace("\r\n", "\n");            MyUDP.UDPClient.BeginReceive(ReceiveCallback, MyUDP);            listView1.Invoke(new AddTolstDiscoveredDevices(AddDiscoveryEntry), ReceiveString);
        }
第一次運行正確,當我關閉Form1後,再次點擊Button時,舊會出現如下錯誤:
Only one usage of each socket address (protocol/network address/port) is normally permitted
我猜測應該是關閉Form1窗體時沒有關閉異步和端口?有知道的幫忙看看!謝謝!

解决方案 »

  1.   

    Socket是非托管资源,必须手工释放。可以在Form1的Close事件中Socket.Close(),最好是公开一个方法,用该方法Socket.Close
      

  2.   

            private void Form1_Closed(object sender, EventArgs e)
            {
               GlobalUDP.UDPClient.Close();
            }
      

  3.   


    這樣的話,在 public void ReceiveCallback(IAsyncResult ar)裡又會產生錯誤!Cannot access a disposed object.
    Object name: 'System.Net.Sockets.Socket'.這個函數是異步
      

  4.   

     protected override void OnClosing(CancelEventArgs e)
    {
       GlobalUDP.UDPClient.Close(); }
    试试这个
      

  5.   

    我也知道要關,而且沒關掉,可問題是我不知道如何才能關掉!HELP
      

  6.   

            if (sock == null)
                {
                    return;
                }
                sock.Blocking = false;
                sock.Shutdown(SocketShutdown.Both);
                sock.Close();
                sock = null;
    试试!!
      

  7.   


      string ReceiveString = Encoding.ASCII.GetString(MyUDP.UDPClient.EndReceive(ar, ref MyUDP.EP));Cannot access a disposed object.
    Object name: 'System.Net.Sockets.UdpClient'.這行出錯了!
      

  8.   

    朋友你解决了,我现在也碰到这个问题,
    你解决了,可否给我解释一下,我的email:[email protected]