2012-03-09 101 views
0

我试图ping一个使用Ping类的服务器,但在10次之后,该方法返回true,我总是收到错误(这意味着服务器关闭了[?],它不是)下面是方法:使用C#ping服务器使用C#

 public bool IsConnectedToInternet() 
    { 
      Ping p = new Ping(); 
      try 
      { 

       PingReply reply = p.Send("www.uic.co.il", 1000); 
       if (reply.Status == IPStatus.Success) 
        return true; 
      } 
      catch(Exception ex) 
      { 
       MessageBox.Show(ex.Message.ToString());  
      } 
      return false; 
    } 

    private void start_Click(object sender, EventArgs e) 
    { 
     for (; ;) 
     { 
      Console.WriteLine(IsConnectedToInternet); 


     } 
    } 

为什么我在一段时间后一直变得虚假? 谢谢。快

for (; ;) 
{ 
    Console.WriteLine(IsConnectedToInternet); 
} 

将循环的请求后,可以发送请求:

+3

您的异常处理是最糟糕的我”在很长一段时间里已经见过。不要理解这个例外:http://blog.gauffin.org/2010/11/do-not-catch-that-exception/。而'IsConnectedToInternet'应该是一个方法'CheckConnection()'而不是属性。 – jgauffin 2012-03-09 13:05:55

+2

请考虑将其移至方法。预计房产没有成本。 – 2012-03-09 13:06:33

+0

在你的'Catch'子句中,尝试'Catch(Exception ex){Console.WriteLine(Ex.Message.toString())}'看看会发生什么。 – 2012-03-09 13:08:01

回答

10

你充斥服务器的请求。

如果你只是编写一个保持活着的服务或服务状态控制,然后使用计时器,每分钟甚至每10分钟应该是足够好的。

另外,正如其他人在他们的评论中指出的那样,通过在获取者中执行ping操作来滥用属性,因为调用可能需要一些时间,并且属性获取者应该真的返回,即使不是立即很快。一个CheckConnection()方法会有更明确的意图。

+0

对不起,但我并不真正了解你在说什么,你说超时应该至少1分钟? – idish 2012-03-09 13:12:01

+0

他说你每10ms轮询一次服务器,如果ping需要这么长时间的话。因此淹没它。 – jgauffin 2012-03-09 13:15:38

+0

@idish - no。你太常打电话了。使用计时器来控制ping何时发生。 – ChrisF 2012-03-09 13:15:58

1

我重写了你的代码。

如果连接丢失,将触发一个名为ConnectionLost的事件,并在连接重新连接时触发一个名为Connected的事件。

public class NetworkStateMonitor 
{ 
    private System.Threading.Timer _timer; 
    bool _wasConnected = false; 

    public NetworkStateMonitor() 
    { 
     _timer = new System.Threading.Timer(OnPing, null, TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10)); 
    } 

    public bool CheckInternetConnection() 
    { 
     bool result = false; 
     Ping p = new Ping(); 
     try 
     { 
      PingReply reply = p.Send("www.uic.co.il", 1000); 
      if (reply.Status == IPStatus.Success) 
       return true; 
     catch (PingException) 
     { 
      return false; 
     } 
    } 


    private void OnPing(object state) 
    { 
     var newState = CheckInternetConnection(); 
     if (!newState && _wasConnected) 
      ConnectionLost(this, EventArgs.Empty); 
     else if (newState && !_wasConnected) 
      Connected(this, EventArgs.Empty); 

     _wasConnected = newState; 
    } 

    public event EventHandler ConnectionLost = delegate{}; 
    public event EventHandler Connected = delegate{}; 
} 
+0

感谢您努力帮助我,我不太了解这个代码,您是否创建了2个活动?这个代码对我有用吗? – idish 2012-03-09 13:23:05

+0

因为超时仍然是1000毫秒,而且我需要多久调用一次这些方法? – idish 2012-03-09 13:24:23

+0

超时时间为1000毫秒,但***轮询间隔***为10秒。您用'for'循环发送服务器垃圾邮件。 – jgauffin 2012-03-09 13:26:39

0

对于其他过这个页面,这个功能将是更好的绊脚石,如果它被改写为:

public bool CheckInternetConnection(string HostName) 
{ 
    bool result = false; // assume error 
    try { 
     Ping oPing = new Ping(); 
     PingReply reply = oPing.Send(HostName, 3000); 
     if (reply.Status == IPStatus.Success){ 
      result = true; 
     } 
    } catch (Exception E) { 
     // Uncomment next line to see errors 
     // MessageBox.Show(E.ToString()); 
    } 
    return result; 
} 

现在拨打使用:

bool IsSuccessful = CheckInternetConnection("www.uic.co.il");