Tag Archives: ping
Tracert implementation in C#
While I was looking for a tracert method that I could use for an application of mine I noticed that the .NET framework doesn’t provide one out of the box, so I decided to create one that matched my requirements.
The method below works in the exact same fashion the tracert method works. Providing
the method with the IP address, the max number of hops you would like to receive and a timeout value for the hops will allow you to track down the route your data will travel in order to get to their destination, as well as the time (in milliseconds) it will take for each hop.
Main method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /// <summary> /// Traces the route which data have to travel through in order to reach an IP address. /// </summary> /// <param name="ipAddress">The IP address of the destination.</param> /// <param name="maxHops">Max hops to be returned.</param> public IEnumerable<TracertEntry> Tracert(string ipAddress, int maxHops, int timeout) { IPAddress address; // Ensure that the argument address is valid. if (!IPAddress.TryParse(ipAddress, out address)) throw new ArgumentException(string.Format("{0} is not a valid IP address.", ipAddress)); // Max hops should be at least one or else there won't be any data to return. if (maxHops < 1) throw new ArgumentException("Max hops can't be lower than 1."); // Ensure that the timeout is not set to 0 or a negative number. if (timeout < 1) throw new ArgumentException("Timeout value must be higher than 0."); Ping ping = new Ping(); PingOptions pingOptions = new PingOptions(1, true); Stopwatch pingReplyTime = new Stopwatch(); PingReply reply; do { pingReplyTime.Start(); reply = ping.Send(address, timeout, new byte[] { 0 }, pingOptions); pingReplyTime.Stop(); string hostname = string.Empty; if (reply.Address != null) { try { hostname = Dns.GetHostByAddress(reply.Address).HostName; // Retrieve the hostname for the replied address. } catch (SocketException) { /* No host available for that address. */ } } // Return out TracertEntry object with all the information about the hop. yield return new TracertEntry() { HopID = pingOptions.Ttl, Address = reply.Address == null ? "N/A" : reply.Address.ToString(), Hostname = hostname, ReplyTime = pingReplyTime.ElapsedMilliseconds, ReplyStatus = reply.Status }; pingOptions.Ttl++; pingReplyTime.Reset(); } while (reply.Status != IPStatus.Success && pingOptions.Ttl <= maxHops); } |