0

I have an hourly diagnostic check that runs on our servers, which includes a check that the server has internet connectivity.

Below is the code I use to ping a url (from http://stackoverflow.com/questions/11800958/using-ping-in-c-sharp), but it's producing intermittent results on both https://www.facebook.com and https://www.google.com. The poor results are actually experienced on both a dedicated hosted VM and a separate Azure VM:

public static bool PingHost(string nameOrAddress)
{
    if (nameOrAddress.IsEmpty())
        return false;

    bool pingable = false;
    Ping pinger = new Ping();
    try
    {
        var hostNameOrAddress = nameOrAddress.Replace("http://", "").Replace("https://", "")
            .Replace("/", "");
        PingReply reply = pinger.Send(hostNameOrAddress);
        pingable = reply.Status == IPStatus.Success;
    }
    catch
    {
        return false;
        // Discard PingExceptions and return false;
    }
    return pingable;
}

What's concerning is that I have other diagnostic checks that test other third party urls, and they show as fine.

What is a more reliable way to check for general internet connectivity?

Savage
  • 2,296
  • 2
  • 30
  • 40
  • To what end? Being able to ping random sites on the net *doesn't* answer the question "can I achieve task X which depends on internet connectivity?". The instant after this method returns `true`, and *before* you can take any action on that result, the external realities may have changed. – Damien_The_Unbeliever Sep 29 '17 at 07:58
  • It's a routine diagnostic, to give you an indication of whether the world is ok - that's good value, and we produce charts based on those numbers. But it looks like there's a decent duplicate answer for me to use, even if there's still some debate on the answers over there. – Savage Sep 29 '17 at 08:21

0 Answers0