Tutto funzionava un'ora fa e molti giorni fa. Il collegamento cerco di ping è:Perché sto ricevendo PingException?
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?
Sei dietro un proxy? – Yahya
Yaha no. Non sono indietro. – user3596190
la riga 33 non è un problema di problema nella richiesta ping. –