public bool PingTest() { var ping = new Ping(); const string adr = "173.194.39.56"; PingReply pingReply = ping.Send(adr, 500); if (pingReply.Status == IPStatus.Success) { return true; } else { return false; } }
/// <summary> /// Pings the host. /// </summary> /// <param name="ipAddress">The ip address.</param> /// <param name="portNumber">The port number.</param> /// <returns></returns> public static bool PingHost(string ipAddress, int portNumber) { bool result = false; try { Uri url = new Uri(ipAddress); string pingurl = String.Format("{0}", url.Host); string host = pingurl; Ping p = new Ping(); PingReply reply = p.Send(host, portNumber); if (reply != null && reply.Status == IPStatus.Success) result = true; } catch (Exception ex) { } return result; }
//thread to do ping public void startPing(string sHost, PingOptions pOptions) { System.Diagnostics.Debug.WriteLine("+++thread started"); string ipAddress = sHost;//make a local copy Ping myPing = new Ping(); PingOptions myPingOptions = pOptions;//make a local copy int iTimeout = pOptions.TimeOut; int _numberOfPings = pOptions.numOfPings; bool doNotFragment = pOptions.DontFragment; int bufferSize = pOptions.bufferSize; byte[] buf = new byte[bufferSize]; //long sumRoundtripTime=0; onReply(new PingReplyEventArgs("ping started...",PingReplyTypes.info)); replyStats _replyStats = new replyStats(ipAddress); PingReply reply = null; try { onReply(new PingReplyEventArgs(pOptions.ToString(), PingReplyTypes.info)); //check DNS first as this may block for long time IPAddress address; HostParms hostParms = new HostParms(ipAddress, 4); try { //is IP address address = IPAddress.Parse(ipAddress); hostParms.isValid = true; } catch { if (checkHost(ref hostParms)) ipAddress = hostParms.ipAddress.ToString(); } if (!hostParms.isValid) throw new PingUnknownHostException("Unkown host: " + ipAddress); for (int ix = 0; ix < _numberOfPings; ix++) { reply = myPing.Send(ipAddress, buf, iTimeout, myPingOptions); string sReply = ""; if (reply.Status == IPStatus.Success) { //sumRoundtripTime += reply.RoundTripTime; _replyStats.add(1, 1, reply.RoundTripTime); sReply = myResources.getReply(reply, ipAddress); } else if (reply.Status == IPStatus.DestinationHostUnreachable) { _replyStats.add(1, 0, reply.RoundTripTime); throw new PingUnknownHostException("Destination unreachable"); } else { _replyStats.add(1, 0, reply.RoundTripTime); sReply = myResources.getReply(reply, ipAddress); } System.Diagnostics.Debug.WriteLine(sReply); onReply(new PingReplyEventArgs(sReply)); } onReply(new PingReplyEventArgs(_replyStats.ToString(), PingReplyTypes.info)); } catch (PingUnknownHostException ex) { System.Diagnostics.Debug.WriteLine("PingUnknownHostException: " + ex.Message); onReply(new PingReplyEventArgs("Unknown host: "+ ipAddress, PingReplyTypes.info)); } catch (PingException ex) { System.Diagnostics.Debug.WriteLine("PingException: " + ex.Message); } catch (ThreadAbortException ex) { onReply(new PingReplyEventArgs("ping failed", PingReplyTypes.info)); Thread.CurrentThread.Abort(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("PingException: " + ex.Message); } finally { onReply(new PingReplyEventArgs("done", PingReplyTypes.done)); System.Diagnostics.Debug.WriteLine("---thread ended"); } }