|
-
Apr 29th, 2009, 01:00 AM
#1
Thread Starter
Frenzied Member
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,
-
Apr 29th, 2009, 01:02 AM
#2
Re: Testing internet connection
-
Apr 29th, 2009, 01:13 AM
#3
Thread Starter
Frenzied Member
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,
-
Apr 29th, 2009, 01:19 AM
#4
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.
-
Apr 29th, 2009, 01:33 AM
#5
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.
-
Apr 29th, 2009, 01:35 AM
#6
Thread Starter
Frenzied Member
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:
Public Function TestServer() As Boolean Dim isAvailable As Boolean = True Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Try sock.Connect("xxx.xxx.xx.xx", xx) Catch ex As SocketException Console.WriteLine(ex.Message) isAvailable = False Catch ex As Exception Console.WriteLine(ex.Message) isAvailable = False Finally If sock.Connected Then sock.Close() End If End Try Return isAvailable 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,
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|