2014-05-09 22 views
6

Tutto funzionava un'ora fa e molti giorni fa. Il collegamento cerco di ping è:Perché sto ricevendo PingException?

Link to ping

Questo è il codice in 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"; 
} 

Quando si sta cercando di eseguire questa linea:

bool bval = nc.PingConnection(satellite_address); 

E 'intenzione di la classe 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; 
     } 
    } 
} 

Nella classe NC quando si cerca di fare la linea:

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

E 'il salto al blocco catch e getta un PingException:

verificata un'eccezione nel corso di una richiesta Ping

E poi in Form1 il risultato che ritorna è che non c'è l'accesso a internet ma c'è internet e posso navigare all'URL senza problemi.

Questo è il messaggio di eccezione completo:

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: 

Linea 33 è:

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

quello che potrebbe essere la ragione per cui questa eccezione presentarsi? non si è mai fatto vedere prima che il mio programma funzioni per alcuni anni ora.

E come o come devo gestirlo?

+0

Sei dietro un proxy? – Yahya

+0

Yaha no. Non sono indietro. – user3596190

+0

la riga 33 non è un problema di problema nella richiesta ping. –

risposta

9

Non è possibile passare un URL completo al metodo Send della classe Ping. Il parametro string hostNameOrAddress deve essere

Una stringa che identifica il computer che è la destinazione per il messaggio echo ICMP. Il valore specificato per questo parametro può essere un nome host o una stringa di un indirizzo IP.

Così si può passare solo a www.sat24.com o IP dell'host 82.94.176.100 (preso dalla linea di comando ping www.sat24.com).

Se si desidera passare un URL completo al metodo, è necessario estrarre l'host da tale URL per eseguire il ping. Per questo caso puoi prendere la classe Uri

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

grazie. errore di battitura nel mio indirizzo IP. una virgola invece di un punto. difficile da individuare! –

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

"No Host sconosciuto"

mia scommessa è che tale host è noto.

Si dovrebbe eseguire il ping "www.sat24.com" non "http://www.sat24.com/ ..."

Ping.Send non lo dice accetta un 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

Problemi correlati