How to check if there is internet connection?

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

How to check if there is internet connection?

Post by sritcp » Sat Jul 06, 2019 2:34 pm

Before accessing, say, an online file, an app has to check if there is an internet connection, right?
Is there an LC command to do this?
tsNetGetStatus(pID) seems to be for a specific transfer, not a general check.

There is a section in the User Guide named "Transferring Information with Files, the Internet and Sockets", but it doesn't give much information. Can someone point me to information on working with internet connections in LC? (other than the dictionary). Thanks.

Sri

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if there is internet connection?

Post by jacque » Sat Jul 06, 2019 5:57 pm

I think the most common way is to just try to get a short file or a url on a known server, and see if you get any errors back.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to check if there is internet connection?

Post by bogs » Sat Jul 06, 2019 7:10 pm

I'm sure she means something simpler than this, but this works :D

Code: Select all

put url("http://www.duckduckgo.com") into tmpUrl

 if tmpUrl is empty then
	 put "Failed"
 else
	 put "Good"
 end if

answer tmpUrl
Selection_002.png
No connection to this site...
Since the site doesn't exist, the variable is empty, same as if you tried to dl a file and had no connection.
Selection_003.png
DUCK!
Of course, this site exists, and so would a file if you have a connection.
Image

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to check if there is internet connection?

Post by sritcp » Mon Jul 08, 2019 2:26 pm

Thanks, Jacque & bogs!

Good enough but we can't know if the site is down or we don't have internet connection.
We can reduce the probability of false positive if we check two websites, I guess.

There must be a webpage with small amount of content but near 100% uptime that one can use for this purpose. www.google.com ?

Regards,
Sri

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: How to check if there is internet connection?

Post by bogs » Mon Jul 08, 2019 3:09 pm

Maybe 'is it down right now' will work for your purposes?
javascript:void(location.href='https://www.isitdownrightnow.com/downor ... tion.host));
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if there is internet connection?

Post by jacque » Mon Jul 08, 2019 5:36 pm

Typically I check a small file on the site the app will be contacting, often just a one-word text file created for that purpose, that just returns the word "true". This won't work if your app acts like a browser under user control, but all of mine are looking for a connection to a specific server.

It doesn't really matter in that case why a failure happens, all the app needs to know is that it can't get the data it needs.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to check if there is internet connection?

Post by sritcp » Mon Jul 08, 2019 7:10 pm

@bogs, thanks for the link. It should do. I'll check it out.

@jacque, yes, I was thinking along the same lines, that the initial act of setting credentials on (say,) AWS should serve as a check (if we are not accessing public web pages on the internet). (I am still learning the intricacies of AWS).

My thinking was that the user is going to be upset if they click on a button expecting data and nothing shows up; they'd be more forgiving if they know that it is their internet. This may be relevant especially for iPad (and other tablet) apps, which are not always connected to a wifi -- a specific case (relevant to my app) is where an itinerant special educator is shuttling between buildings or even schools several times in a day. In any case, your method is adequate, for all practical purposes.

Thanks,
Sri

P.S. : An aside, I used the square bracket around some text in my post (in addition to parenthesis) and it completely messed up the formatting!!! Forgot it is reserved for Bulletin-Board Formatting!

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to check if there is internet connection?

Post by trevix » Thu Feb 03, 2022 12:31 pm

I have the same problem (checking connection to internet fail proof) and also the need to find out the public IP of the device.
So I am using this code that works.
Unfortunately my Standalone, on launch, has to load a lot of stuff so I would like to find a faster way to do it.
May be a PHP script on my web site?
Please comment and make suggestion on the following code.

Code: Select all

--check internet connection
--FIND connection and pubblic IP
function CheckConnection --check connection and find pubblic IP
     put false into sConnectionTrue
     CheckGetMyIP --get the IP
     if sMyIP is not empty then
          put true into sConnectionTrue
     end if
     return sMyIP
end CheckConnection

command CheckGetMyIP
     put empty into sMyIP --no IP
     repeat with U = 1 to 4
          if sMyIP is not empty  then 
               put word 1 of  (last word of sMyIP) into sMyIP
               replace "</body></html>" with empty in sMyIP
               if ":" is in sMyIP then exit repeat --IPv6
               put sMyIP into tTest
               replace "." with empty in tTest
               if tTest is  a number and "<" is not in tTest then
                    exit CheckGetMyIP
               end if
          end if
          load URL ("http://checkip.dyndns.org") with message "CheckGetMyIPreceived"
          wait 300 milliseconds with messages
          if U > 2 then 
               Show widget "SpinnerCentral" of group "SplashImage" of card "CD_SplashImage" of stack "Referi"
          end if
     end repeat 
     --did not work, try a different URL
     put empty into sMyIP --no IP
     Show widget "SpinnerCentral" of group "SplashImage" of card "CD_SplashImage" of stack "Referi"
     repeat with U = 1 to 4
          if sMyIP is not empty  then 
               put line 1 of sMyIP into sMyIP
               if ":" is in sMyIP then 
                    put empty into sMyIP
                    exit repeat --IPv6
               end if
               put sMyIP into tTest
               replace "." with empty in tTest
               if  tTest is  a number and "<" is not in tTest then
                    exit CheckGetMyIP
               else
                    put empty into sMyIP --no IP
               end if
          end if
          load URL ("http://ifconfig.me/ip") with message "CheckGetMyIPreceived"
          wait 300 milliseconds with messages
     end repeat 
end CheckGetMyIP

command CheckGetMyIPreceived  pURL, pURLStatus
     if pURLStatus is "cached" then
          put URL pURL into sMyIP
          unload url pURL
     else
          put empty into sMyIP
     end if
end CheckGetMyIPreceived
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if there is internet connection?

Post by jacque » Thu Feb 03, 2022 8:56 pm

You could use shell to get the public IP. This sitehttps://www.cyberciti.biz/faq/how-to-fi ... n-a-linux/ gives the commands for three different DNS servers. I used Google to test and it works:

Code: Select all

put shell("dig TXT +short o-o.myaddr.l.google.com @ns1.google.com")
This would not only give the IP, but would return an error if there is no internet connection. The command is only for Unix-based systems, including OS X, but there's probably something similar for Windows.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

Re: How to check if there is internet connection?

Post by andresdt » Thu Feb 03, 2022 9:04 pm

In addition to all the solutions given here, you can also use the url that the Firefox browser uses for this purpose.

Code: Select all

function internetConnection 
   return url("http://detectportal.firefox.com/") is "success"
end internetConnection

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to check if there is internet connection?

Post by trevix » Thu Feb 03, 2022 10:39 pm

Forgot to say: mobile only, iOS and Android
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: How to check if there is internet connection?

Post by mrcoollion » Fri Feb 04, 2022 10:41 am

This is my code and it uses Google. I guess you can use 'detectportal.firefox.com' as well.

Code: Select all

on mouseUp
   if CheckInternetConnection () is true
   then
      answer "Your internet connection seems ok."
   else
      answer "No connection"
   end if
end mouseUp

function CheckInternetConnection
   -- Use like this : if CheckInternetConnection () is true ..................
   put "ping -n 1 google.com" into pCommandLines
   set the hideConsoleWindows to true
   put shell(pCommandLines) into tAnswer
   if tAnswer contains "could not find" then put False into tAnswer else put True into tAnswer
   return tAnswer
end CheckInternetConnection
Regards,

Paul

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How to check if there is internet connection?

Post by FourthWorld » Fri Feb 04, 2022 3:53 pm

sritcp wrote:
Mon Jul 08, 2019 2:26 pm
...we can't know if the site is down or we don't have internet connection.
Does the site you need access to belong to you or a third party?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7228
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How to check if there is internet connection?

Post by jacque » Fri Feb 04, 2022 7:28 pm

trevix wrote:
Thu Feb 03, 2022 10:39 pm
Forgot to say: mobile only, iOS and Android
Ah. No shell then. I think the best answer is your suggestion to use a php script on the server. If you didn't need the IP it would be simpler.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

trevix
Posts: 960
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: How to check if there is internet connection?

Post by trevix » Mon Feb 07, 2022 5:49 pm

So this apparently works:

Code: Select all

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo 'User IP Address_'.$_SERVER['REMOTE_ADDR']; ?>
 </body>
</html>
Is there any circumstances, beside not being connect or having the host down, when it could not work? (Proxy wich I know nothing?)
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “Internet”