//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"); } }
//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"); } }