Best way to determine Internet Connection (Vote/Advice)

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 Return True Else Return False end if

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

Dim ipAddress As String ipAddress = System.Network.LookupIPAddress("google.com") If ipAddress <> "" Then Return True Else Return False End If

  1. (Apparently this only tells you if the computer is connected to a LAN)
System.Network.IsConnected
  1. (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 True
else
  return False
end if

#else
return False
#endif
End Function[/code]

[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
Return False[/code]

Dim socket As New HTTPSocket socket.yield = True dim ih as InternetHeaders = (socket.GetHeaders("http://www.google.com", 1)) return (ih <> nil)

just tried them … number 2 is my favorite

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.

Well if you target corporate networks the thing isn’t easy.

You may need to use a proxy without DNS.
So you request is done by proxy and you can only query the page you want and react on errors.

Ping may be blocked. Port 80 could be blocked and only port 443 being open for selected domains.

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.

Great. Thanks for clarifying.

Or perhaps there is a way to do port 80 check first and if that fails use a second method that works in a different way?

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.

Thanks
sb

If you’re going to use #2, then use a TCPSocket instead of relying on the HTTPSocket exposing some of its underlying methods (which it really shouldn’t).

Can you explain what you mean by this please Tim because I don’t understand.

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?

What I’m saying is

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.

I understand now. Thanks.

I’d be tempted to perform an API call to Ipify https://www.ipify.org/ which will also return the public ip address of the network.

Interesting.

Not sure I understand how this relates to determining whether user has internet connection or just network connection.

If you get a response from and internet site like www.ipify.org, then you definitely have internet connection. Wayne is saying that as a bonus, you also get your public address, too.

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.

How about a DNS lookup, maybe for a fictitious address? No reply = no Internet connection, any reply = connected.