How to check if there is internet connection?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
How to check if there is internet connection?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to check if there is internet connection?
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
HyperActive Software | http://www.hyperactivesw.com
Re: How to check if there is internet connection?
I'm sure she means something simpler than this, but this works
Since the site doesn't exist, the variable is empty, same as if you tried to dl a file and had no connection.
Of course, this site exists, and so would a file if you have a connection.
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
Re: How to check if there is internet connection?
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
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?
Maybe 'is it down right now' will work for your purposes?
javascript:void(location.href='https://www.isitdownrightnow.com/downor ... tion.host));
javascript:void(location.href='https://www.isitdownrightnow.com/downor ... tion.host));
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to check if there is internet connection?
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.
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
HyperActive Software | http://www.hyperactivesw.com
Re: How to check if there is internet connection?
@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!
@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?
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.
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>
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to check if there is internet connection?
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:
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.
Code: Select all
put shell("dig TXT +short o-o.myaddr.l.google.com @ns1.google.com")
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: How to check if there is internet connection?
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?
Forgot to say: mobile only, iOS and Android
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>
-
- Posts: 722
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: How to check if there is internet connection?
This is my code and it uses Google. I guess you can use 'detectportal.firefox.com' as well.
Regards,
Paul
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
Paul
-
- VIP Livecode Opensource Backer
- Posts: 9857
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to check if there is 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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to check if there is internet connection?
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
HyperActive Software | http://www.hyperactivesw.com
Re: How to check if there is internet connection?
So this apparently works:
Is there any circumstances, beside not being connect or having the host down, when it could not work? (Proxy wich I know nothing?)
Code: Select all
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<?php echo 'User IP Address_'.$_SERVER['REMOTE_ADDR']; ?>
</body>
</html>
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>