Results 1 to 6 of 6

Thread: Testing internet connection

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Testing internet connection

    Hello,

    I am using this code to test for an internet connection. As my application will have to login to a web server. However, if the user internet connection was to fail or cable pulled out. I will have to notify the user.
    Code:
     ' Ping www.google.com to check if the user has a internet connection.
    		Public Function PingTest() As Boolean
    			Dim ping As New Ping()
    
    			Dim pingStatus As PingReply = ping.Send(IPAddress.Parse("208.69.34.231"))
    
    			If pingStatus.Status = IPStatus.Success Then
    				Return True
    			Else
    				Return False
    			End If
    		End Function
    The only way I think I can test for an Internet connection is to ping www.google.com. So I have a used a server timer which I have set for 1/2 second and in the lapsed event it will call this ping function. If the ping returns false. Then my app will take appropriate action.

    Do you think using google as a way to test an Internet connect is a good thing. If google was to fail, then my app would not function. Is polling 1/2 second to much or too little? Just wondering about my whole idea if it is good or not?
    Many thanks,
    steve

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Testing internet connection

    Was there a question?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Testing internet connection

    Hello,

    It was the last paragraph.

    And I was just wondering if pinging www.google.com was a good idea to test for an active internet connection.

    Thanks,
    steve

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Testing internet connection

    You must have edited your post after I loaded it because I swear that that wasn't there when I read it.

    Have you considered using the IsNetworkAvailable property and NetworkAvailabilityChanged event. They relate network availability and not internet availability specifically but they are really your only choice other than try to access an internet resource and catching the failure. If you do poll, is it really necessary to do it so often? Surely every few seconds at the most would be enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Testing internet connection

    This may be a daft question, but why ping Google and not the actual web server you are intending to access?

    I know Google is pretty ubiquitus but there must be a small risk that for some reason google is down while your server is still up.

    I'd agree with jmcilhinney - polling so frequently seems overkill, maybe poll the IsNetworkAvailable property every few seconds and every few minutes ping a website.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: Testing internet connection

    Hello,

    I have already tried the classes for Network class. However, the app could be connected to the networkAvailability always return true, even though there is no Internet.

    I cannot ping to the actual web server as the ping (icmp) has been disabled for security reasons. So I created a socket and tested if I could connect on the IP and port number on the web server instead.

    vb Code:
    1. Public Function TestServer() As Boolean
    2.             Dim isAvailable As Boolean = True
    3.             Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
    4.  
    5.             Try
    6.                 sock.Connect("xxx.xxx.xx.xx", xx)
    7.             Catch ex As SocketException
    8.                 Console.WriteLine(ex.Message)
    9.                 isAvailable = False
    10.             Catch ex As Exception
    11.                 Console.WriteLine(ex.Message)
    12.                 isAvailable = False
    13.             Finally
    14.                 If sock.Connected Then
    15.                     sock.Close()
    16.                 End If
    17.             End Try
    18.             Return isAvailable
    19. End Function

    This worked but it was taking a long time to get a response back. Up to 5 seconds. The client felt this was too slow.

    This is why was trying to ping google, as the response from the ping was very fast. However, I am not sure if pinging another website is a good idea just to see if the client has an active Internet connection. Google could fail.

    Thanks,
    steve

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