C# : Ping Class in .NET Framwork 2.0 or above

This example show how to ping to particular host or site and how to get the detail information.

The below code gets the status using synchronous method.

Complete C# code:
----------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.NetworkInformation;

namespace MemInfo
{
class Program
{      
static void Main(string[] args)
{
Ping pingObj = new Ping();
string data = "pinging";
byte[] buffer = Encoding.ASCII.GetBytes(data);
PingReply pReply = pingObj.Send("www.eggheadcafe.com", 10000, buffer);

if (pReply.Status == IPStatus.Success)
{
Console.WriteLine("Address: " + pReply.Address.ToString());
Console.WriteLine("Round Trip: " + pReply.RoundtripTime.ToString());
Console.WriteLine("Buffer Length: " + pReply.Buffer.Length);
Console.WriteLine("Time To Live: " + pReply.Options.Ttl.ToString());
}
else
            {
                 Console.WriteLine("Could not connected: " + pReply.Status);
}

Console.Read();
}

Output:
-------
for www.eggheadcafe.com I got :

Could not connected: TimedOut

for www.google.com I got:
Address: 209.85.153.104
Round Trip: 27
Buffer Length: 7
Time To Live: 247

You can see http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx for asynchronous call.

By Perry    Popularity  (2841 Views)