Results 1 to 12 of 12

Thread: [RESOLVED] [2005] Check for internet connection?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Resolved [RESOLVED] [2005] Check for internet connection?

    Hello!

    When my application loads, it pings a website, and downloads an .xml file to check for a newer version of the program. However, i quickly found out, this resulted in an error if either a firewall is blocking this, or if the internet connection is disconnected. So, i put in some "if"'s and "else"'s. This partially solved the problem.

    But on computers with no internet connection at all (as in not disconnected connections, but with no connections), an unhandled exception is thrown when the ping is run as there are no connections present. So is there a way to fix this, so that if there are no connections available, it just skips the whole thing?

    Some code

    VB Code:
    1. If My.Computer.Network.Ping("http://connect-utb.byethost8.com", 1000) Then                  My.Computer.Network.DownloadFile(remotefile, localfile)  
    2. Else
    3. End if

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: [2005] Check for internet connection?

    Here is some code that detects whether a URL is accessible, its best to check two or three well known sites (such as google, yahoo etc.) in order to determine if a connection is available.

    VB Code:
    1. Public Function CheckURLExists(ByVal site As String) As Boolean
    2.         Dim objUrl As New System.Uri(site)
    3.         Dim objWebReq As System.Net.WebRequest
    4.         Dim objResp As System.Net.WebResponse
    5.         objWebReq = System.Net.WebRequest.Create(objUrl)
    6.         Dim Success As Boolean
    7.         Try
    8.             objResp = objWebReq.GetResponse
    9.             objResp.Close()
    10.             Success = True
    11.         Catch ex As Exception
    12.             Success = False
    13.         End Try
    14.         objWebReq = Nothing
    15.         Return Success
    16.     End Function
    Note that there is an oddity with this function, I'm not sure whether this is a bug in the IDE or what, but it always catches an exception correctly when run after a build, but the try/catch does not always catch an exception when run within the IDE.
    Last edited by Bulldog; Jan 28th, 2007 at 05:11 PM.

  3. #3
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: [2005] Check for internet connection?

    Put the code in a Try/Catch statement?

    Since the code given above has an oddity about it when run in the IDE, why not just replace it with your ping code?

  4. #4
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    Re: [2005] Check for internet connection?

    This is what I do:

    VB Code:
    1. Private Function internetConnection() As Boolean
    2.  
    3.         Dim url As New System.Uri("http://www.yoursite.com/")
    4.         Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    5.         Dim response As System.Net.WebResponse = request.GetResponse()
    6.  
    7.         Try
    8.             response.Close()
    9.             request = Nothing : Return True
    10.         Catch ex As Exception
    11.             request = Nothing : Return False
    12.         End Try
    13.  
    14.     End Function


    Syntax:

    VB Code:
    1. If internetConnection() = True Then
    2.             Label1.Text = "Connected"
    3.         Else : Label1.Text = "Not Connected"
    4.         End If
    Last edited by BrendanDavis; Jan 28th, 2007 at 09:01 PM.
    God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.

    I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: [2005] Check for internet connection?

    Thanks for reply, but sadly, this did not solve my problem. Again, if my program is used on a pc with no internet connection available what so ever (not disconnected or connect, just no internet connections at all, as in "Network Connections" showing up empty.. just to get the point through), an unhandled exception is thrown.

    EDIT: This also happens if the internet connection is disabled..

    Anyone?

    Cheers!
    Last edited by Untouchab1e; Feb 5th, 2007 at 06:24 PM.

  6. #6
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: [2005] Check for internet connection?

    I suggested the Try/Catch statement. If the user has the condition which you describe, then you can fairly assume they don't have internet access. If the code you're trying is causing an error when the condition you describe occurs, then a Try/Catch statement should work? Are you using a Try/Catch statement?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: [2005] Check for internet connection?

    Im using the Code BrendanDavis posted

    VB Code:
    1. Try
    2.             response.Close()
    3.             request = Nothing : Return True
    4.         Catch ex As Exception
    5.             request = Nothing : Return False
    6.         End Try

    So yes, im using a try/catch statement..

    I get the following error when trying to run it with no internet connection:

    The remote name could not be resolved
    Last edited by Untouchab1e; Feb 5th, 2007 at 06:32 PM.

  8. #8
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: [2005] Check for internet connection?

    Exactly which line causes your error?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: [2005] Check for internet connection?

    VB Code:
    1. Dim response As System.Net.WebResponse = request.GetResponse()

  10. #10
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: [2005] Check for internet connection?

    Try moving that line to within the Try/Catch statement. If you're using his code exactly, it's outside of the Try/Catch...

    EDIT: For that matter, try moving the entire bit of the function code (all the declarations) to within the try/catch statement.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Aug 2006
    Posts
    115

    Re: [2005] Check for internet connection?

    That solved it! Thank you so much!

  12. #12
    Addicted Member
    Join Date
    Dec 2006
    Location
    Georgia
    Posts
    170

    Re: [RESOLVED] [2005] Check for internet connection?

    Glad to finally be able to help someone instead of being the one bening helped.

    For my future copy-and-pasting:

    VB Code:
    1. Private Function internetConnection() As Boolean
    2.  
    3.         Dim url As New System.Uri("http://www.yoursite.com/")
    4.         Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    5.         Dim response As System.Net.WebResponse = request.GetResponse()
    6.  
    7.         Try
    8.             Dim url As New System.Uri("http://www.yoursite.com/")
    9.             Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
    10.             Dim response As System.Net.WebResponse = request.GetResponse()
    11.             response.Close()
    12.             request = Nothing : Return True
    13.         Catch ex As Exception
    14.             request = Nothing : Return False
    15.         End Try
    16.  
    17.     End Function

    VB Code:
    1. If internetConnection() = True Then
    2.             Label1.Text = "Connected"
    3.         Else : Label1.Text = "Not Connected"
    4.         End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width