Ping is used to test the reachability of a host on an Internet Protocol(IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer. Microsoft .Net provides Ping class which is present in System.Net.NetworkInformation Namespace to ping a machine.
Ping class provide Send() method with Hostname/Address and timeout as a parameter. Send() method returns PingReply object which provides information about the ICMP(Internet Control Message Protocol) echo message if message was received or it will provides the reason for the failure if no message was received. If ICMP echo message is not received within time specified in the timeout parameter than Status property will be set to TimedOut. If an attempt to send an ICMP echo request and receive the corresponding ICMP echo reply Successfully than Status property is set as Success.
Using the properties of PingReply Class, We can find Address, Roundtrip Time, Status etc.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.NetworkInformation;
-
- namespace PingApplication
- {
- class Program
- {
- static void Main(string[] args)
- {
- string hostname = "www.bing.com";
- int timeout = 10000;
- Ping ping = new Ping();
- try
- {
- PingReply pingreply = ping.Send(hostname, timeout);
- if (pingreply.Status == IPStatus.Success)
- {
- Console.WriteLine("Address: {0}", pingreply.Address);
- Console.WriteLine("status: {0}",pingreply.Status);
- Console.WriteLine("Round trip time: {0}", pingreply.RoundtripTime);
- }
- }
- catch (PingException ex)
- {
- Console.WriteLine(ex);
- }
- Console.ReadKey();
- }
- }
- }
Preview
Hope you like it. Thanks