how to check the internet connectivity

My computer in connected to a LAN which inturn is connected to the internet through the gateway router. so the IP address i use is the private IP address.

I want to check the internet connectivity from c#. How can i know whether or not the internet connectivity is working although my LAN connection is working very well Please help me with this problem.



Answer this question

how to check the internet connectivity

  • TCben

    While mechanisms exist for determining if there is a network connection in the 2.0 Framework, the same methods aren’t very useful when it comes to seeing if you have an actual internet connection and not just a local area connection.

    One of the easiest things to do would be to use the Ping Class to send out a few hello’s to a server (or servers) of your choice and see if you get a legit reply back.

    One little problem with Ping though is that it isn’t always the most useful because some ISP’s and networks block ICMP traffic (which Ping relies on).

    Another couple of methods you might try (maybe as a stage 2 if Ping fails you) is to use connect to some external server... like a web server using the HttpWebRequest or WebClient and if successful, you know you’ve got an outbound connection (unless there are some crazy proxy things going on) or do a DNS lookup of a remote hostname with GetHostByName or GetHostEntry.



  • sdesmoulin

    NetworkInterface.GetIsNetworkAvailable(), described in the article (which only tests if the network is available; not the Internet, directly), eventually uses wininet.dll's InternetGetConnectedState() which can be PInvoked as follows


    [DllImport("wininet.dll")]
    internal static extern bool InternetGetConnectedState(ref uint flags, uint dwReserved);

    uint num1 = 0;
    Boolean isNetworkConnected =
    UnsafeWinINetNativeMethods.InternetGetConnectedState(ref num1, 0);

    If you want to be informed of a network address change you'll have to use Ws2_32.dll's WSAEventSelect() function. Again, this really doesn't have a direct corellation to internet connectivity, unless the computer is connected directly to the modem/router.



  • alexmaster_2004

    A recent Coding For Fun article You Can Take it With You, Part 2 discusses this type of thing.

  • Justin0509

    I suppose that you want to check if your application can connect to specific URL

    I use this function to check if deployed application has access to URL

    public static bool IsOnLine()
    {
       bool flag1;
       WebResponse response1;
       WebRequest request1;
       try
       {
          Uri uri1 = new Uri(@http://www.microsoft.com);
          request1 = WebRequest.Create(uri1);
          response1 = request1.GetResponse();
          response1.Close();
          request1 = null;
          flag1 = true;
       }
       catch (Exception exception2)
       {
          ProjectData.SetProjectError(exception2);
          Exception exception1 = exception2;
          response1 = null;
          request1 = null;
          flag1 = false;
          ProjectData.ClearProjectError();
       }
       return true;
    }
    

    hope this helps



  • jonpfl

    Well the article "Coding4Fun You Can Take It With You, Part 2.htm" was useful but it was coded using C# express and i use .net framework 1.1 so i think i cannot switch to new version for this task. Is there any better way

    The "WebRequest" and "WebResponse" classes seems to be helpful but it checks for a URL what if that particular server is down but still there is an internet connection.


  • how to check the internet connectivity