PING in c#

Ping in C#

Hello friends, We have already explain regarding Call method after specific time interval C#Alert from code behind asp.net,required field validator in asp.net,Difference between RegisterClientScriptBlock and RegisterStartupScript asp.netDifference between ref and out parameters. Today I am going to explain regarding Ping in C#.

PING in c#
PING in c#

PING command in general is used to find whether remote has been connected with Server or not.  Here we have PING class in .net framework.

We have different overloading methods available named with SEND() for sending request to Server and get response from server to remote machine. The overloading methods which are listed as below:

Sr. No Method
1 public PingReply Send( IPAddress address/string host)
2 public PingReply Send(
IPAddress address/string host,
int timeout
)
3 public PingReply Send(
IPAddress address/string host,
int timeout,
byte[] buffer
)
4 public PingReply Send(
IPAddress address/string host,
int timeout,
byte[] buffer,
PingOptions options
)

Where,

Address:   IP address of server

Host: Host name of server, ex: technothirsty.com

Timeout: specifies the maximum number of milliseconds (after sending the echo message) to wait for echo reply message from server

Buffer: contains data to be sent with the echo message and returned in the echo reply message. Maximum 65,500 bytes would be allowed.

Options: A PingOptions object used to control fragmentation and Time-to-Live values for the echo message packet.

Example of PING in C#:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;

namespace PingApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Timers.Timer time = new System.Timers.Timer();
            time.Interval = 10000;
            time.Elapsed += new System.Timers.ElapsedEventHandler(testMethod);
            time.Start();
            testMethod(time, null);
            Console.ReadKey();
        }

        private static void testMethod(object sender, System.Timers.ElapsedEventArgs e)
        {

            try
            {
                Ping p = new Ping();
                string str = "test";
                var pingResponce = p.Send("technothirsty.com", 1000, Encoding.ASCII.GetBytes(str));
                if (pingResponce.Status == IPStatus.Success)
                    Console.WriteLine("Address:{0}, RoundTrip:{1}, TTL:{2}, Status:{3}", pingResponce.Address, pingResponce.RoundtripTime, pingResponce.Options.Ttl, pingResponce.Status);
                else if (pingResponce.Status == IPStatus.TimedOut)
                    Console.WriteLine("Timeout...!!!");
                else
                    Console.WriteLine("Error Occurred ...!!!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error Occurred ...!!!");
            }
        }
    }
}

Output:

PING-output
PING-output

Thanks for reading. So guys don’t forgot to share your feedback.Happy Coding and stay thirsty for technology.

Leave a Reply