I’m writing an app that relies on having an Internet connection and I want to quickly detect if the user has a connection. In other posts on this forum I have seen this code that works well, but if takes about 30 seconds to complete.
//Checks to see if you are on internet
Dim IPAddress As String
IPAddress = System.Network.LookupIPAddress("wikipedia.org")
If IPAddress <> "" Then
msgBox "Connected to Internet"
Else
msgbox "Not connected to Internet"
End If
I was wondering if anyone knows a quicker way to detect a connection so the user does not have to wait around?
Don’t use LookupIPAddress. I had an issue with it recently. However it manages to find the address it doesn’t trigger LittleSnitch. LookupIPAddress can work, even if network connections are not allowed. I notified LittleSnitch, but they kind of dismissed me, so
As much as I warn people about synchronous requests, this app absolutely required network connection, so I wasn’t against using one to test for it when the app starts.
// Test internet conneciton using HTTPSocket to see if we can get through firewalls
// Using a synchronous connection with a timeout of 5s because it's google and we should reach it.
dim oSock as new HTTPSocket
dim sGoogleText as String = oSock.Get("https://google.com", 5)
if sGoogleText = "" or oSock.ErrorCode <> 0 then
// Network attempt failed, handle that here.
end
This will bring your maximum wait down from 30s to 5s. This is a synchronous request though, so your app will lock up while it waits!
Tim, I don’t know if I’m doing this right. I put your code on a button and ran it.
Modified Code:
[code] dim oSock as new HTTPSocket
dim sGoogleText as String = oSock.Get("https://google.com", 5)
if sGoogleText = "" or oSock.ErrorCode <> 0 then
// Network attempt failed, handle that here.
msgBox "No Internet Connection"
Else
msgBox "You are connected to the Internet"
End if[/code]
It took 35 seconds. If its suppose to take 5 seconds, I’m doing something wrong. Where is the code have to go?
I just tried this code in a button and it took less than a second to come back…Not sure why’s it taking 35 seconds on your part…of course, I’m on a fiber connection so I’m sure that has something to do with it also
another thing you have to be worried about is if the DNS response is a “splash page” instead of the read response. Like when you go to coffee shops/hotels/airports/etc and you have to “login” to their page before you get to the real internet.
this comes out to be the fastest way to test internet access :
Dim http As New HTTPSocket
HTTP.Address = "www.apple.com"
http.port = 80
http.Connect
if http.LastErrorCode = 0 then
Return True
Else
Return False
end if
http.close
also this one :
Function InternetIsConnected() As Boolean
Dim http As New HTTPSocket
http.yield = True
dim ih as InternetHeaders = (http.GetHeaders("http://www.google.com", 1))
return (ih <> nil)
End Function
The thing is, when I test this, I’m not connected to the Internet. Thats sort of the objective. It detects that the user does not have an Internet connection and then warns them to connect without crashing my app.
Right, but I’m lost as to why the timeout is adding 30 seconds when you’re not connected. The timeout is supposed to be when it “gives up”. Maybe there’s something internal I’m not aware of.
So I guess let’s ask some debug questions.
What OS / Version?
What IDE?
Have you tried any of this with a new framework socket?
When you say “not connected” for your test,
Ethernet plug pulled? WiFi off?
Blocker tool like LittleSnitch?
dim sGoogleText as String = oSock.Get("https://google.com", 5)
I’m using MacOS 10.11.6
Xojo 2016 4.1
The Internet is down where I’m at right now, so I’m working from a Personal Hotspot from an iPhone to get Internet. When I’m testing the code, I’m connected to a WiFi Router with no Internet. Waiting around here waiting for the Cable guy to come.
Jean’ code below also takes over 30 seconds.
Dim http As New HTTPSocket
HTTP.Address = "www.apple.com"
http.port = 80
http.Connect
if http.LastErrorCode = 0 then
Return True
Else
Return False
end if
http.close
I usually launch this function in a separate thread, right after starting the application. While the thread is running I do the rest of the initialization stuff. Works perfect. If the function returns a wan-address, you’re sure you have connection to the internet.
[code]Private Function GetPublicIP() As String
// Obtain public IP
// Attention: do this within a separate thread since it takes a roundtrip to get ip
Any extra delay is probably due to lookup of the name apple.com , google.com , etc.
HTTPSocket will have DNS try to find the number for the address first, then attempt to connect.
Pick a known, reliable address that doesn’t require any DNS lookup.
I’ve used this:
[code]Function CheckOnline() as Boolean
Dim ret As Boolean = False
Dim sh As New Shell
sh.Mode = 2
If TargetWin32 Then
sh.Timeout = -1
sh.Execute “ping -w 5000 -n 1 8.8.8.8”
Else
sh.Execute “ping -n -q -W 5 -c 1 8.8.8.8”
End If
Do
App.DoEvents(25)
Loop Until Not sh.IsRunning
If sh.Errorcode=0 Then // success
ret = True
End If
Return ret
End Function
[/code]
Google’s DNS server is at 8.8.8.8 and replies to pings.
[quote=322060:@John A Knight, Jr]Any extra delay is probably due to lookup of the name apple.com , google.com , etc.
HTTPSocket will have DNS try to find the number for the address first, then attempt to connect.
Pick a known, reliable address that doesn’t require any DNS lookup.
I’ve used this:
[code]Function CheckOnline() as Boolean
Dim ret As Boolean = False
Dim sh As New Shell
sh.Mode = 2
If TargetWin32 Then
sh.Timeout = -1
sh.Execute “ping -w 5000 -n 1 8.8.8.8”
Else
sh.Execute “ping -n -q -W 5 -c 1 8.8.8.8”
End If
Do
App.DoEvents(25)
Loop Until Not sh.IsRunning
If sh.Errorcode=0 Then // success
ret = True
End If
Return ret
End Function
[/code]
Google’s DNS server is at 8.8.8.8 and replies to pings.[/quote]
some of the cafes/coffee shops/hotels/airports/bus terminals blocked ping (both ICMP and TCP based) but allowed access to those sites. When not on a home network, you cant count on alot of protocols. DNS and HTTP/HTTPS is the exceptions. I was recently at a coffee shop that blocked all ports except DNS, HTTP & HTTPS. and if you did HTTP/HTTPS to ports other than 80/443 it was blocked. Yeah right! their admin was a little paranoid.
My thanks to everyone who tried to help me. I really appreciate it.
John’s code actually works perfectly. There was about a 1 to 2 second delay. That will work just fine.
Scott’s concern that John’s code will not work in coffee shops and cafes because blocked ping may not be an issue since the app will be used on a home network in firehouses. Unless the firehouse blocks the ping, there should be no problem.
[quote=322060:@John A Knight, Jr]Any extra delay is probably due to lookup of the name apple.com , google.com , etc.
HTTPSocket will have DNS try to find the number for the address first, then attempt to connect.[/quote]
You are right @John A Knight, Jr , but @scott boss is also right and that’s why I prefer going via ports 80/443 .
Since I am testing in a thread prior to initialization of the program, no worries about x ms seconds delay.