I have trawled this forum and have found many threads about this topic but none of them seem to categorically propose an ideal and fool proof solution to determine an internet connection for both Mac and Windows.
As such I have listed the various solutions I have found and would be extremely appreciateive if people could vote on the one they think is best and perhaps explain why.
Also, I will actually be using the new framework xojo.net.HTTPSocket for better security and for HTTP 1.1 protocol so please let me know if any of these solutions or any others will not work for that.
dim h as new HTTPSocket
call h.Get("http://www.google.com/", 5)
if h.HTTPStatusCode = 200 then
Dim http As New HTTPSocket
HTTP.Address = "www.apple.com"
http.port = 80
if http.LastErrorCode = 0 then
Dim ipAddress As String
ipAddress = System.Network.LookupIPAddress("google.com")
If ipAddress <> "" Then
(Apparently this only tells you if the computer is connected to a LAN)
(Not for Mac)
[code] Function CheckInternet() As Boolean #if TargetWin32 then
Declare Function InternetGetConnectedState Lib “WinInet” ( ByRef state as Integer, reserved as Integer ) as Boolean
Dim n as Integer
if InternetGetConnectedState( n, 0 ) then
return False #endif
[code] Dim sh As New Shell
sh.Mode = 0
sh.Execute(“ping ns1.google.com -n 1”)
If InStr(0, sh.Result, “Pinging”) > 0 Then Return True
Dim socket As New HTTPSocket
socket.yield = True
dim ih as InternetHeaders = (socket.GetHeaders("http://www.google.com", 1))
return (ih <> nil)
6 does not work on a mac, neither do 5, and 4 as you say does not test internet
1 always returns false for me.
the deal is between 2 3 and 7
7 is 100x slower than 3 that is 10x slower than 2
so 2 is my favorite.
Thanks for testing that Jean-Yves and for your feedback. Much appreciated. It may well be that I use one method for Mac and one for Windows, depending what people think is the best route for each platform.
if port 80 is locked, then you can consider you don’t have access to the internet …
proxy can be considered as stored in the system preferences ? so the http.connect should work if configured in the system.
no if port 80 fails, then use the 443
but as I already said, if port 80 is locked on a computer, then you can consider this computer has a very restricted internet access !
then a call to the computer security team to unlock the ports you need is a necessity.
I used to use a method that would do a DNS look up on 3 different sites (google.com, apple.com and dont remember the third, it might have been xojo.com) and if they answers were the same then I was a network but not on the internet.
HTTPSocket is a subclass of TCPSocket, built for a specific purpose. It really shouldn’t expose many of the TCPSocket’s methods. Note that the new framework does not expose the Connect method, so you won’t have a choice.
I’m sorry, I must be a little dumb because I don’t understand what you’re getting at. I am using xojo.net.HTTPSocket for my actual connection methods but just need to use example 2 (HTTPSocket) to determine whether or not an internet connection is available. I will not be using any more code than what is displayed in the example. As such, are you still saying I should replace the HTTPSocket instance with TCPSocket instead for some kind of better security?
a) philosophically, it is better to not use HTTPSocket.Connect, because it is not logically part of an HTTPSocket. It just happens to be exposed from the underlying TCPSocket class, but it probably shouldn’t be.
b) practically, example #2 just flat out will not work with Xojo.Net.HTTPSocket, as it no longer exposes the Connect method.
My original objection was, Don’t do that because it might not be supported in the future. And then I realized that in fact, it no longer is.
Something to consider (which makes this topic OH so much more interesting imho). All of the techniques described here could still give you a false negative if there were some sort of internet outage going on which only affected one of the backbone providers. For instance, Level3 had an outage last summer which made it impossible for me (in Raleigh, NC) to reach a server in Chicago. Now that doesn’t mean that I didn’t have an Internet connection. I could still chat with the rest of the engineering team, just not reach a database server.
What I’m saying is that it might be in your interest to define what “has an internet connection” means in your particular context because whether or not the user has access to your servers is a very different question from whether they have access to everything on the internet. You should also consider that connections can come and go in so far that a server that is online and available when your app launches may not be there when your app goes to use it 30 minutes later.