public ICMP_PingResult Ping(String host, Int32 count = 4, Int32 timeout = 1000) { bool infinite = false; ICMP_PingResult Result = new ICMP_PingResult(); // Resolve the specified IP address IPAddress hostIP; try { hostIP = Dns.Resolve(host).AddressList[0]; } catch (Exception e) { throw new Exception("ICMP_Ping: Unable to resolve the specified hostname.", e); } Result.hostIP = hostIP; // Start pinging try { Icmp icmp = new Icmp(hostIP); TimeSpan ret; while (infinite || count > 0) { try { ret = icmp.Ping(timeout); if (ret.Equals(TimeSpan.MaxValue)) { Result.Status = ICMP_Status.TimeOut; } else { Result.Status = ICMP_Status.Success; Result.RoundtripMS.Add(ret.TotalMilliseconds); } if (1000 - ret.TotalMilliseconds > 0) { Thread.Sleep(1000 - (int)ret.TotalMilliseconds); } } catch (Exception e) { throw new Exception("Network Error", e); } if (!infinite) { count--; } } } catch { throw new Exception("Error while pinging the specified host."); } return(Result); }
public ICMP_PingResult Ping(String host, Int32 count = 4, Int32 timeout = 1000) { bool infinite = false; ICMP_PingResult Result = new ICMP_PingResult(); // Resolve the specified IP address IPAddress hostIP; try { hostIP = Dns.Resolve(host).AddressList[0]; } catch (Exception e) { throw new Exception("ICMP_Ping: Unable to resolve the specified hostname.",e); } Result.hostIP = hostIP; // Start pinging try { Icmp icmp = new Icmp(hostIP); TimeSpan ret; while(infinite || count > 0) { try { ret = icmp.Ping(timeout); if (ret.Equals(TimeSpan.MaxValue)) Result.Status = ICMP_Status.TimeOut; else { Result.Status = ICMP_Status.Success; Result.RoundtripMS.Add(ret.TotalMilliseconds); } if (1000 - ret.TotalMilliseconds > 0) Thread.Sleep(1000 - (int)ret.TotalMilliseconds); } catch (Exception e) { throw new Exception("Network Error",e); } if (!infinite) count--; } } catch { throw new Exception("Error while pinging the specified host."); } return Result; }
void UpdateEncoderList() { _event.WaitOne(); _event.Reset(); foreach (ListViewItem lvi in listView1.Items) { if (_done) break; StreamingEncoder encoder = (StreamingEncoder)lvi.Tag; OysterEncoderController.Control oec = new OysterEncoderController.Control(encoder.ControlAddress, encoder.ControlPort); lvi.SubItems[3].Text = "Connecting"; Application.DoEvents(); if (oec.Open()) { if (oec.IsRecording) { lvi.SubItems[2].Text = "Yes"; lvi.BackColor = Color.Cornsilk; Application.DoEvents(); } else { lvi.SubItems[2].Text = "No"; lvi.BackColor = Color.White; Application.DoEvents(); } oec.Close(); lvi.SubItems[3].Text = "Ready..."; lvi.Font = new Font(lvi.Font, FontStyle.Regular); Application.DoEvents(); } else { IPAddress ipAddress; if (encoder.ControlAddress.Split(new char[] {'.'}).Length != 3) { IPHostEntry hostEntry = Dns.GetHostByName(encoder.ControlAddress); ipAddress = hostEntry.AddressList[0]; } else { byte[] addressBytes = ASCIIEncoding.ASCII.GetBytes(encoder.ControlAddress); ipAddress = new IPAddress(addressBytes); } Icmp ping = new Icmp(ipAddress); if (ping.Ping() > TimeSpan.FromMilliseconds(5000.0)) { lvi.SubItems[2].Text = "???"; lvi.SubItems[3].Text = "Possibly disabled. Needs to be power cycled."; lvi.ForeColor = Color.LightGray; lvi.Font = new Font(lvi.Font, FontStyle.Bold); Application.DoEvents(); } else { lvi.Font = new Font(lvi.Font, FontStyle.Regular); lvi.SubItems[2].Text = "Yes"; lvi.SubItems[3].Text = "Encoder refused connection. Usually means it is recording..."; lvi.BackColor = Color.Cornsilk; Application.DoEvents(); } } oec = null; Application.DoEvents(); } _event.Set(); Application.DoEvents(); }