.NET Framework 2.0 carries a lot of good for developers. And as part of my exploration, I came across the neat Ping class - your managed implementation for the ICMP call to check the availability of a server on TCP/IP network. Implemented in the System.Net.NetworkInformation namespace, all it takes is just two lines of code to get the work done.
Below is the source to a quick .NET ping utility I wrote in the past 7 minutes:
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Net;
using
System.Net.NetworkInformation;
#endregion
namespace
PingDotNET
{
class Program
{
static void Main(string[] args)
{
// display the details
Console.WriteLine("Ping .NET - by Gaurav Khanna [gkhanna@gmail.com]");
Console.WriteLine("Written using .NET FX 2.0\n");
// Did we get the address/domain name to ping?
if (args.Length == 0)
{
Console.WriteLine("Usage: PingDotNET ");
return;
}
// instantiate the Ping class
Ping refPing = new Ping();
PingReply refPingResult = null;
// Invoke the "correct" Ping.Send overload [and we do blocking call]...
Console.WriteLine("Pinging {0}...\n",args[0]);
refPingResult = refPing.Send(args[0]);
Console.WriteLine("Ping Status: {0}",refPingResult.Status.ToString());
Console.WriteLine("RoundTrip Time [millisec]: {0}",refPingResult.RoundTripTime);
if (refPingResult.Options != null)
Console.WriteLine("TTL: {0}",refPingResult.Options.Ttl.ToString());
}
}