2014-05-09 106 views
6

這一切都在一小時前和許多天前工作。 我嘗試ping鏈接是:爲什麼我得到PingException?

Link to ping

這是在Form1的代碼:

nc = new NetworkConnection(); 
bool bval = nc.PingConnection(satellite_address); 

if (bval) 
{ 
    label19.Visible = true; 
    label19.Text = "Internet Access"; 
} 
else 
{ 
    label19.Visible = true; 
    label19.Text = "No Internet Access"; 
} 

當它試圖執行這一行:

bool bval = nc.PingConnection(satellite_address); 

這將nc類別:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.Net.NetworkInformation; 
using System.IO; 
using System.Windows.Forms; 

namespace mws 
{ 
    class NetworkConnection 
    { 
     public NetworkConnection() 
     {  
     } 

     public bool PingConnection(string url) 
     { 
      bool Result = false; 

      using (Ping pp = new Ping()) 
      { 
       byte[] buffer = Encoding.ASCII.GetBytes("samplestring"); 
       int timeout = 120; 

       try 
       { 
        PingReply reply = pp.Send(url, timeout, buffer); 
        if (reply.Status == IPStatus.Success) 
         Result = true; 
       } 
       catch (Exception) 
       { 
        Result = false; 
       } 
      } 
      return Result; 
     } 
    } 
} 

在NC類嘗試做線的時候:

PingReply reply = pp.Send(url, timeout, buffer); 

它跳到catch塊,並拋出一個PingException:

ping請求

期間發生異常

然後在Form1中,返回的結果是沒有互聯網接入,但有互聯網,我可以衝浪到URL沒有問題。

這是完整的異常消息:

System.Net.NetworkInformation.PingException was caught 
    HResult=-2146233079 
    Message=An exception occurred during a Ping request. 
    Source=System 
    StackTrace: 
     at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options) 
     at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer) 
     at mws.NetworkConnection.PingConnection(String url) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\NetworkConnection.cs:line 33 
    InnerException: System.Net.Sockets.SocketException 
     HResult=-2147467259 
     Message=No such host is known 
     Source=System 
     ErrorCode=11001 
     NativeErrorCode=11001 
     StackTrace: 
      at System.Net.Dns.GetAddrInfo(String name) 
      at System.Net.Dns.InternalGetHostByName(String hostName, Boolean includeIPv6) 
      at System.Net.Dns.GetHostAddresses(String hostNameOrAddress) 
      at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options) 
     InnerException: 

第33行是:

PingReply reply = pp.Send(url, timeout, buffer); 

可能是什麼,這種異常出現的原因是什麼?它在我的計劃現在爲某些yeras工作之前沒有出現。

我該怎麼處理它?

+0

你是否在代理? – Yahya

+0

Yaha no。我不落後。 – user3596190

+0

第33行不是問題,在ping請求中。 –

回答

9

你不能將完整的URL傳遞給Ping類的Send方法。參數string hostNameOrAddress需要爲

一個字符串,用於標識ICMP回顯消息的目標計算機。爲此參數指定的值可以是主機名稱或IP地址的字符串表示形式。

所以你只能通過在www.sat24.com或主機82.94.176.100(從命令行ping www.sat24.com採取)的IP。

如果您想將完整的URL傳遞給您的方法,您需要從該URL中提取主機以執行Ping。對於這種情況下,你可以採取Uri類

Uri uri = new Uri(url); 
PingReply reply = pp.Send(uri.Host, timeout, buffer); 
+0

謝謝。在我的IP地址中輸入錯字。逗號而不是句號。很難找到! –

0
PingReply reply = pp.Send(url, timeout, buffer); 

「沒有這樣的主機被稱爲」

我敢打賭,沒有這樣的主機是已知的。

你應該平 「www.sat24.com」 而不是 「http://www.sat24.com/ ...」

Ping.Send並不表示它接受一個URL

public PingReply Send(
    string hostNameOrAddress, 
    int timeout, 
    byte[] buffer 
) 

hostNameOrAddress A String that identifies the computer that is the destination for the ICMP echo message. The value specified for this parameter can be a host name or a string representation of an IP address. 

http://msdn.microsoft.com/en-us/library/ms144954(v=vs.110).aspx