简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM