public void ThreadProcess(object state) { while (true) { try { IpHelper helper = new IpHelper(LastIp); Thread thread = new Thread(new ParameterizedThreadStart(helper.GetIp)); thread.Start(); if (!thread.Join(TimeSpan.FromMinutes(1.0))) { _logger.Error("get ip timeout", new object[0]); } string ip = helper.IP; _logger.Info("get ip:{0} - {1}", new object[] { LastIp, ip }); InformationReceived?.Invoke(this, new MessageEventArgs($"{DateTime.Now.ToLongTimeString()}:动态域名获取本机最新IP:{ip}")); if (ip != LastIp) { var ipChangedArgs = new IpChangedEventArgs(LastIp, ip); LastIp = ip; _logger.Info("change ip 1:{0}", new object[] { ip }); IPChanged?.Invoke(this, ipChangedArgs); } } catch (Exception exception) { _logger.Error("ThreadProcess error:{0}-{1}", new object[] { Local, exception }); } finally { Thread.Sleep(TimeSpan.FromMinutes(3.0)); } } }
private string CheckHttp(MonitorHistory h, Config.MonitorConfig item) { string str = "unknow"; int result = 0x3e7; try { TcpClient client2 = new TcpClient { ReceiveTimeout = 0x1388, SendTimeout = 0x1388 }; TcpClient client = client2; client.Connect(item.Ip, item.Port); NetworkStream stream = client.GetStream(); string domain = item.Domain; if (item.Subdomain != "@") { domain = string.Format("{0}.{1}", item.Subdomain, item.Domain); } string s = string.Format("GET / HTTP/1.1\r\nHOST:{0}\r\n\r\n", domain); byte[] bytes = Encoding.ASCII.GetBytes(s); stream.Write(bytes, 0, bytes.Length); byte[] buffer = new byte[0x400]; int count = stream.Read(buffer, 0, 0x400); if (!int.TryParse(Encoding.ASCII.GetString(buffer, 0, count).Substring(9, 3), out result)) { str = "down"; } else { str = "ok"; } client.Close(); } catch (Exception) { str = "down"; } h.WriteData(DateTime.Now, str == "down"); InformationReceived?.Invoke(this, new MessageEventArgs($"{DateTime.Now.ToLongTimeString()}:监控结果:ip={item.Ip},status={str},statuscode={result}")); return(str); }