|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article demonstrates BackgroundI have a pretty unreliable Internet connection over wireless, so packets are being lost all the time. From time to time my access point pretends to be dead or, not-so-rarely, my ISP's servers go down. To identify the problem, I usually have to run a few ping commands in different windows and that really annoys me. The other disadvantage of the ping command is that it doesn't provide statistics until you stop it. So, I decided to make a simple GUI application which will provide needed information as quickly as possible. CodeThe 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 I stated at the beginning of the article you can use
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 the first half of the story. Based on that simple method, we need to provide some meaningful and useful statistical information. The class has five // constructs pinger for localhost (127.0.0.1)
public HostPinger()
// constructs pinger from XML config file
public HostPinger(XmlNode node)
// use DNS to resolve the IP address from hostname
public HostPinger(string hostName)
// construct pinger for host without a name
public HostPinger(IPAddress address)
// it the IP address specified, hostname can be used as a description
public HostPinger(string hostName, IPAddress address);
Pinging is controlled with
User can also provide a logger to log these events [except public interface IPingLogger
{
void LogStart(HostPinger host);
void LogStop(HostPinger host);
void LogStatusChange(HostPinger host, HostStatus oldStatus, HostStatus newStatus);
}
And finally the StatisticsThe information about the host that this class can provide to the user is as listed below:
Host Information, Ping Options and Configuration FileHost pinger can be configured with following options:
The application uses hosts.cfg file located in the same directory as executable file to store list of hosts for pinging and ping options in XML format. <pinger>
<!-- List of host -->
<host>
<!-- Host options -->
<name><!-- host name --></name>
<ip><!-- host ip --></ip>
<timeout><!-- timeout --></timeout>
<interval><!-- ping interval --></interval>
<pingsbeforedead><!-- pings before dead --></pingsbeforedead>
<buffersize><!-- echo message size --></buffersize>
<ttl><!-- Time To Live --></ttl>
<dontfragment><!-- Don't fragment flag --></dontfragment>
<description><!-- Description of the host --></description>
<dnsinterval><!-- Duration of interval between DNS queries --></dnsinterval>
<recenthistorydepth><!-- Depth of recent history --></recenthistorydepth>
</host>
<host>
<!-- Host options -->
<!-- ... -->
</host>
<!-- More host... -->
</pinger>
Example<pinger>
<host>
<name>localhost</name>
<ip>127.0.0.1</ip>
<timeout>300</timeout>
<interval>2000</interval>
<pingsbeforedead>5</pingsbeforedead>
<buffersize>32</buffersize>
<ttl>3</ttl>
<dontfragment>false</dontfragment>
</host>
<host>
<name>google.com</name>
<timeout>2000</timeout>
<interval>2000</interval>
</host>
<host>
<name>some local host</name>
<ip>192.168.0.1</ip>
<timeout>500</timeout>
<interval>2000</interval>
</host>
<host>
<name>yahoo.com</name>
<timeout>2000</timeout>
<interval>2000</interval>
</host>
</pinger>
History
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||