Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VB .NET Comnectivity check and error handling  (Read 5551 times)

0 Members and 1 Guest are viewing this topic.

alecsillidan

    Topic Starter


    Beginner

    VB .NET Comnectivity check and error handling
    « on: October 23, 2012, 01:29:33 PM »
    Hello again.
    I'm working on a project and I need to check if the computer has internet access.
    The Network.IsAvailable is not good for the project since many computers connect through routers and some routers might not be connected.
    I tried with the Ping method, if it's connected it works fine, but i'm getting an unhandled exception "cannot connect to host" if not connected.

    My question is: how can I check if the computer is connected and when it is online to start executing the rest of the code.
    Also, how can I disable unhandled exceptions? If an exception occurs then the program continues executing without error or "not responding".

    Thank you!
    We never had to cheat 'cause we already won!

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VB .NET Comnectivity check and error handling
    « Reply #1 on: October 23, 2012, 04:38:51 PM »
    use Ping. The way to "disable" unhandled exceptions is to handle them.
    I was trying to dereference Null Pointers before it was cool.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VB .NET Comnectivity check and error handling
    « Reply #2 on: October 23, 2012, 04:55:12 PM »
    Another option: since ping only really tests if a machine is responding and doesn't always correspond with the availability of HTTP requests, we can do the same thing that win Vista/7 do to check the network connection:


    Code: [Select]
    public bool ConnectionAvailable()
            {
                try
                {
                    var result = new WebClient().DownloadString("http://www.msftncsi.com/ncsi.txt");
                    return true;

                }
                catch (Exception exx)
                {
                    return false;
                }
               

            }

    Which I think would translate to something like the following for VB.NET:

    Code: [Select]
    Public Function ConnectionAvailable() As Boolean
    Try
        Dim Result As String = (new WebClient).DownloadString("http://msftncsi.com/ncsi.txt")
        return true
    Catch exx As Exception
        return false
    End Try
    End Function
    I was trying to dereference Null Pointers before it was cool.