Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a "ping" inside of my C# application

Tags:

c#

ping

I need my application to ping an address I'll specify later on and just simply copy the Average Ping Time to a .Text of a Label.

Any help?

EDIT:

I found the solution in case anyone is interested:

Ping pingClass = new Ping();        
PingReply pingReply = pingClass.Send("logon.chronic-domination.com");
label4.Text = (pingReply.RoundtripTime.ToString() + "ms");
like image 211
Sergio Tapia Avatar asked Aug 15 '09 04:08

Sergio Tapia


People also ask

How do I ping myself in CMD?

In Windows, hit Windows+R. In the Run window, type “cmd” into the search box, and then hit Enter. At the prompt, type “ping” along with the URL or IP address you want to ping, and then hit Enter.

How do I ping an address in C#?

Using ping in C# is achieved by using the method Ping. Send(System. Net. IPAddress) , which runs a ping request to the provided (valid) IP address or URL and gets a response which is called an Internet Control Message Protocol (ICMP) Packet.

Can I ping an IP address?

Running Ping Using Command Prompt (Windows) Run the command by typing “ping” along with the IP address or domain name you want to check. In this example, we're entering ping 8.8. 8.8.


1 Answers

Give a look the NetworkInformation.Ping class.

An example:

Usage:

PingTimeAverage("stackoverflow.com", 4);

Implementation:

public static double PingTimeAverage(string host, int echoNum)
{
    long totalTime = 0;
    int timeout = 120;
    Ping pingSender = new Ping ();

    for (int i = 0; i < echoNum; i++)
    { 
        PingReply reply = pingSender.Send (host, timeout);
        if (reply.Status == IPStatus.Success)
        {
            totalTime += reply.RoundtripTime;
        }
    }
    return totalTime / echoNum;
}
like image 196
Christian C. Salvadó Avatar answered Oct 16 '22 14:10

Christian C. Salvadó