在我的 C# 应用程序内部进行“ping”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1281176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:37:57  来源:igfitidea点击:

Making a "ping" inside of my C# application

c#ping

提问by Sergio Tapia

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.

我需要我的应用程序 ping 稍后指定的地址,只需将平均 Ping 时间复制到标签的 .Text 即可。

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

采纳答案by CMS

Give a look the NetworkInformation.Pingclass.

看看NetworkInformation.Ping类。

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

回答by schoetbi

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 上已经有一个项目正在做你想要的。这还包括 ICMP (RFC 792) 的实现

Sourceforge Project

Sourceforge 项目