Best way to check internet connection

Hi. What is the best method for checking for an internet connection (desktop)? I am using AirTable and need to make sure there is an internet connection before it can connect to the table. I searched the forum and found a couple of suggestions, and this is the one I am contemplating using now:

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)

Is there a better one?

This looks half decent.

It depends if people can get to google in the areas that you’re deploying to (i.e. not china).

It also depends if their modem/dns looks up and returns a result in 1 second or less, might want to up that 1 to 10.

If you’re checking for a connection to airtable, why not try the connection to airtable.com ?

This will be better than a ping for what you need as there’s no guarantee that ping isnt blocked at the users site.

You could also use your standard call to airtable (httpsocket.get?) and see if that times out, saving yourself a wasted call/bandwidth on some headers that you will be throwing away (this is dependent on how often you do this).

No. Try what you want to do and see if you succeed.
With firewalls, proxies and maybe a non available DNS server, you may be able to download something, but can’t get connection to google.

I tend to use ipify https://www.ipify.org/, their very simple api also gives me a public IP address that could be useful elsewhere.

It is more or less useless to test for an internet connection prior to connecting to “the real thing”. The connection can fail at any time during “the real thing”. Deal with that scenario.

Thanks for the replies everyone. I agree with seeing that the app can connect to AirTable since that’s what it has to connect to anyway. I’ll give it a go and see what happens

Yeah the usual issues is that if you dont just try connecting to the thing you actually want to use you can get false positives
You ping of some other server says “hey this should wrk” but the service isn’t responding to your software for some reason

Look at the frequency of things like people posting on this forum about “Hey is this web site (or documentation.xojo.com) up for you ?” where some people can reach the service & others cant.

Its where you get services like http://downforeveryoneorjustme.com
Which begs the question of what happens when you cant reach that ? :stuck_out_tongue:

[quote=334471:@Norman Palardy]

Its where you get services like http://downforeveryoneorjustme.com
Which begs the question of what happens when you cant reach that ? :P[/quote]

I will have to post here asking if anyone can reach downforeveryoneorjustme.com

:stuck_out_tongue:

[quote=334471:@Norman Palardy]Its where you get services like http://downforeveryoneorjustme.com
Which begs the question of what happens when you cant reach that ? :stuck_out_tongue:
[/quote]
You go to http://downforeveryoneorjustmedotcomdownforeveryoneorjustme.com :stuck_out_tongue:

Just test to see if you can buy new Xojo licenses.
If you end up with new Xojo licenses then success, you have internet! lol

[quote]Just test to see if you can buy new Xojo licenses.
If you end up with new Xojo licenses then success, you have internet! lol[/quote]
:slight_smile: great!

IMHO the best way is to download a small stub file from a known source. Put a text file on your own server where you know the contents, then download it and read the data to verify it’s correct.

Just because you receive a response from google doesn’t mean you’re receiving a response from google. For example, you could be actually reaching the router’s log-in page.

You need to verify content to make sure that a valid connection exists.

Simultaneously, @Eli Ott makes a great point that any net code needs to be robust enough to handle a failure at any point during the connection.

On Windows:

InternetGetConnectedState function

[quote=335788:@Kato Gangstad]On Windows:

InternetGetConnectedState function[/quote]

This API call is not reliable. It will return true when there is a LAN connection but no Internet connection.

@Wayne Golding

Hello Sir, care to share how you implement to get the public IP from ipify? Thank you!

Nevermind, Sir Wayne. I got it:

Dim socket As new HTTPSocket()
Dim response As String

response = socket.Get(“https://api.ipify.org”, 15)

msgbox response

Thanks for leading me to it.

[quote=335811:@Dale Dedoroy]@Wayne Golding

Hello Sir, care to share how you implement to get the public IP from ipify? Thank you![/quote]

If you have a website, you can easily implement this on your own. Create a file with text editor with the following and name it as a .shtml file, example getmyip.shtml. Must be .shtml otherwise won’t work. Upload to your website. The file return just your public IP address. You capture that. Of course you can also use it with your browser.

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <title>Get External IP Address</title>
   </head>
   <body>
      <!--#echo var="REMOTE_ADDR" -->
   </body>
</html>

Or, if shtml is not working but php does, then you can create for instance a file named index.php with this contents and copy it to a subfolder on your webserver:

[code]<?PHP

function getUserIP()
{
$client = @$_SERVER[‘HTTP_CLIENT_IP’];
$forward = @$_SERVER[‘HTTP_X_FORWARDED_FOR’];
$remote = $_SERVER[‘REMOTE_ADDR’];

if(filter_var($client, FILTER_VALIDATE_IP))
{
    $ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
    $ip = $forward;
}
else
{
    $ip = $remote;
}

return $ip;

}

$user_ip = getUserIP();

echo $user_ip; // Output IP address [Ex: 177.87.193.134]

?>[/code]

https://dharma.ch/myip

Why not test using the socket to connect to airtable if it provides an error, inform the user there is a problem.