Sergio Tapia
Sergio Tapia

Reputation: 41138

Making a "ping" inside of my C# application

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");

Upvotes: 14

Views: 39124

Answers (2)

schoetbi
schoetbi

Reputation: 12856

Just as a sidenote to this. There is allready a project on sourceforge doing about that what you want. This has also included an implementation of ICMP (RFC 792)

Sourceforge Project

Upvotes: 0

Christian C. Salvadó
Christian C. Salvadó

Reputation: 827366

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;
}

Upvotes: 35

Related Questions