简体   繁体   中英

How Can i Deal with Ping Exception in c#

I've created a simple method to check if my Internet connection is active. Unfortunately, the try section becomes overlooked because of System.Net.NetworkInformation.PingException” in System.Net.Ping.dll. As well noticed, the problem occurs with PingReply. Could you tell me how to deal with this excepion and allow code to run correctly so to execute try section.

MyFunction:

    private void ConnectionStatusChecker(object source, ElapsedEventArgs e)
    {

        Ping myPing = new Ping();
        String host = "https://www.google.com/";
        byte[] buffer = new byte[32];
        int timeout = 1000;
        PingOptions pingOptions = new PingOptions();

        Ping ping = new Ping();
        try
        {
            PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
            if (reply.Status == IPStatus.Success)
                ConnectionStatus = "Connected";
            else
                ConnectionStatus = "Disconnected";
        }
        catch (System.Net.NetworkInformation.PingException)
        {
            ConnectionStatus = "Disconnected-Exception";
        }
    }

Edit: I would like to store in ConnectionStatus Variable the information about my Internet Connection. Try section describes the value in case of beeing disconnected or connected. But PingException does not allow me to check it - exception automatically blocks try section. The thing is, how to deal with this exception, where is the source or mistake I made?

You should log InnerException to see whats really going on:

catch (PingException ex)
{
    Console.WriteLine(ex.InnerException);
    ConnectionStatus = "Disconnected-Exception";
}

Which outputs the following output:

System.Net.Sockets.SocketException (11001): No such host is known.
   at System.Net.Dns.InternalGetHostByName(String hostName)
   at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
   at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)

And fails for the ping command as well(making sure the results are expected):

PS C:\Users\user> ping https://www.google.com/
Ping request could not find host https://www.google.com/. Please check the name and try again.

Which means that the host https://www.google.com/ is not enabling ICMP replies for HTTPS. You can have a look at Can you get a reply from a HTTPS site using the Ping command? for a good explanation as to why this is happening.

You can also verify this in System.Net.NetworkInformation.Ping.Send() documentation:

PingException

An exception was thrown while sending or receiving the ICMP messages. See the inner exception for the exact exception that was thrown.

Instead, try to ping the hostname www.google.com :

Ping myPing = new Ping();
String host = "www.google.com";
byte[] buffer = new byte[32];
int timeout = 1000;
PingOptions pingOptions = new PingOptions();

string ConnectionStatus;

Ping ping = new Ping();
try
{
    PingReply reply = myPing.Send(host, timeout, buffer, pingOptions);
    if (reply.Status == IPStatus.Success)
        ConnectionStatus = "Connected";
    else
        ConnectionStatus = "Disconnected";
    }
catch (PingException ex)
{
    Console.WriteLine(ex.InnerException);
    ConnectionStatus = "Disconnected-Exception";
}

Console.WriteLine(ConnectionStatus);

Which outputs no exceptions and the connection succeeds:

Connected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM