日期:2014-05-18 浏览次数:21757 次
 public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Listen));
            thread.IsBackground = true;
            thread.Start();
           
           /* online = new OnListen(Listen);
            this.BeginInvoke(online);*/
        }
        System.Net.IPAddress ipAddress = System.Net.IPAddress.Parse("192.168.0.74");
        string str = string.Empty;
        public delegate void OnListen();
        OnListen online;
        void Listen()
        {
            System.Threading.Thread.Sleep(100);
              //   while (true)
            
                IPHostEntry ipHost = Dns.Resolve("192.168.0.74");   //运行一段时间后,这句出现问题 :An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll   
             
   IPAddress ipAddr = ipHost.AddressList[0];
                str = ipAddr.ToString(); //System.Net.Dns.GetHostByAddress(ipAddress).HostName.ToString();
                
                if (str != "")
                {
                    label1.Text = string.Format("已连接,{0}", DateTime.Now);
                }
                else
                { label1.Text = string.Format("断开连接,{0}", DateTime.Now); }
                str = null;
                Listen();
            
        }