Page 1 of 2

How to check if there is internet connection?

Posted: Sat Jul 06, 2019 2:34 pm
by sritcp
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

Re: How to check if there is internet connection?

Posted: Sat Jul 06, 2019 5:57 pm
by jacque
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.

Re: How to check if there is internet connection?

Posted: Sat Jul 06, 2019 7:10 pm
by bogs
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.

Re: How to check if there is internet connection?

Posted: Mon Jul 08, 2019 2:26 pm
by sritcp
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

Re: How to check if there is internet connection?

Posted: Mon Jul 08, 2019 3:09 pm
by bogs
Maybe 'is it down right now' will work for your purposes?
javascript:void(location.href='https://www.isitdownrightnow.com/downor ... tion.host));

Re: How to check if there is internet connection?

Posted: Mon Jul 08, 2019 5:36 pm
by jacque
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.

Re: How to check if there is internet connection?

Posted: Mon Jul 08, 2019 7:10 pm
by sritcp
@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!

Re: How to check if there is internet connection?

Posted: Thu Feb 03, 2022 12:31 pm
by trevix
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

Re: How to check if there is internet connection?

Posted: Thu Feb 03, 2022 8:56 pm
by jacque
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.

Re: How to check if there is internet connection?

Posted: Thu Feb 03, 2022 9:04 pm
by andresdt
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

Re: How to check if there is internet connection?

Posted: Thu Feb 03, 2022 10:39 pm
by trevix
Forgot to say: mobile only, iOS and Android

Re: How to check if there is internet connection?

Posted: Fri Feb 04, 2022 10:41 am
by mrcoollion
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

Re: How to check if there is internet connection?

Posted: Fri Feb 04, 2022 3:53 pm
by FourthWorld
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?

Re: How to check if there is internet connection?

Posted: Fri Feb 04, 2022 7:28 pm
by jacque
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.

Re: How to check if there is internet connection?

Posted: Mon Feb 07, 2022 5:49 pm
by trevix
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?)