Code Tracer and Net Pinger
Introduction
This article demonstrates the usage ofPing
class located in the System.Net.NetworkInformation
namespace in different scenarios such as host monitoring, tracing
routes and scanning range of IP addresses. In addition to these network
tools, the application also tracks statistics about tracked hosts and
allows the user to display those statistics as graphs.Ping Class
The most common way to ping another host is to send ICMP [RFC792] echo message and then wait for the response from the targeted host. As it was stated at the beginning of the article, you can usePing
class available in .NET Framework that wraps this type of ICMP message
to make your life easier and save yourself from implementing the
protocol by yourself.Ping
class goes together with some other classes and data types such as PingOptions
and PingReply
classes and IPStatus
enumeration. The heart of the class is the Send
method [and SendAsync
]
that sends echo message and waits for the response. The method has
several overloads, but all of them return an instance of the PingReply
class which contains results of echo request [Status
property] and response time [RoundtripTime
]. Here are a few examples: Ping pinger = new Ping();
// first parameter is TTL and second sets flag in IP header to
// tell routers not to fragment the datagram
PingReply reply = PingOptions pingerOptions = new PingOptions(127, false);
// specify computer by its name
PingReply reply = pinger.Send("localhost");
// with timeout specified
PingReply reply = pinger.Send("localhost", 1000);
// specify computer by its IP address
PingReply reply = pinger.Send(new IPAddress(new byte[] { 127, 0, 0, 1 }));
// specify computer by its name, sets timeout, uses user defined buffer and
// options
PingReply reply = pinger.Send(new IPAddress(new byte[] { 127, 0, 0, 1 }),
1000, buffer, pingerOptions);
But it is only one half of the story. Based on this simple method, we
need to provide some meaningful and useful statistical information.IPRouteTracer Class
Traceroute is implemented byIPRouteTracer
class and each hop [router] by IPRouteHop
class. Hop class contains IP address of the router, its ordinal number in the path and response times of each ping request.
IPRouteTracer
's constructors accept IP address of the
destination host and various parameters: request timeout period, number
of pings and number of retries for each hop. In each tracer, try sends
specified number of ping request before it starts another try or
progresses to the next hop. List of detected hops is exposed by Route
property.Downlaod Link:
0 Commentaires