C#-ping超过超时,但报告成功 [英] c# - ping exceeds timeout but reports success

查看:520
本文介绍了C#-ping超过超时,但报告成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

在我的代码中,我将ping超时设置为100ms

in my code, I set a timeout of a ping to 100ms

new Ping().Send(item._deviceIP, 100, new byte[1])

可正确ping并正确答复,但IPStatus.TimeExceeded为故障",并且在RTT> 100ms后报告成功.

which pings correctly and replies correctly, but the IPStatus.TimeExceeded is "faulty" and reports a success after the RTT is > 100ms

应如何处理:

在收到回应时,如果IPStatus为:

when recieving a pingreply, if the IPStatus is a:

  • TimeExceeded(> 100ms),_ devicePing的颜色应设置为红色

  • TimeExceeded (>100ms), _devicePing should have a color set to Red

成功(<= 100ms),_ devicePing的颜色应设置为绿色

Success(<=100ms), _devicePing should have a color set to green

设置了其他合适的颜色.

any other, appropriate color is set.

会发生什么:

任何ping回复,即使RTT> 100ms也是成功的回复报告

Any ping reply which is a reply report success even with a RTT >100ms

类设备

{
    public IPAddress _deviceIP;
    public string _deviceMAC;
    public string _deviceName;
    public PingReply _devicePing ;
    public Color _deviceStatus = Color.White;
    public int _timeout_Count = 0;

    public Color deviceStatus
        {
            get { return _deviceStatus; }
            set { _deviceStatus = value; }
        }

主程序代码

List<device> _alive = new List<device>();
foreach (device item in _clients)
{
    PingReply _client_reply = new Ping().Send(item._deviceIP, 100, new byte[1]);
    item._devicePing = _client_reply; (item object accepts a PingReply)
    IPStatus _client_status = _client_reply.Status;
    switch (_client_status)
    {
        case IPStatus.TimeExceeded:
        {
            item.deviceStatus = Color.Red;
        }        
        break;

    //rest of code

推荐答案

在所有情况下,您都需要检查PingReply的状态. 在为超时指定非常小的数字时,Ping会回复即使超时毫秒已过去也可以接收.

You will need to check the status of the PingReply in all cases. When specifying very small numbers for timeout, the Ping reply can be received even if timeout milliseconds have elapsed.

现在您问一个困难的问题-什么是500ms,250ms,100ms 10ms的非常小的数字?很高兴您提出要求..请参阅本文,以解决该问题. https://msdn.microsoft.com/en -us/library/ms144954%28v = vs.110%29.aspx

And now you ask the difficult question - what is a very small number 500ms, 250ms, 100ms 10ms, ??? Glad you asked .. see this article for no answwer to that question. https://msdn.microsoft.com/en-us/library/ms144954%28v=vs.110%29.aspx

这篇关于C#-ping超过超时,但报告成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆