Internet Check

Post Reply
User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Internet Check

Post by rbytes » Wed Nov 21, 2018 6:56 pm

This program demonstrates a method to error check so that you don't get a crash when trying to download from a website that is offline or misspelled, or when your internet connection is lost.

The function within it can be adapted for use in any project that reads data from a website.

Code: Select all

/*
Internet Check
by rbytes, November 2018
Uses PING to check internet status
refname$ should be a website that is always up, such as "www.apple.com"
-If chk is 2, internet is live and the website name$ is active 
-If chk is 1, internet is live, as indicated by successful
 PING of website refname$, but the website name$ is not active
-If chk is 0, the internet is disconnected
My tests indicate that pinging an active website with "[name].com" or "www.[name].com"
returns 1, but pinging it with "http://[name].com" or "https://[name].com" always returns 0
You must use the form PING([name],port), with port=80 to get a response of 1.

To test this program:
a. Run it with name$ and refname$ set to active websites. chk will = 2
b. Run it with name$ deliberately misspelled. chk will = 1
c. Run it with your device in Airplane mode. chk will = 0
*/

chk=intchk("x-rates.com","www.apple.com")
choice=chk+1
on choice goto 20,30,40
20 PRINT "You are not connected to the internet"
   GOTO skip
30 PRINT "Your website is not responding"
   GOTO skip
40 PRINT "Your website is responding"
skip:
end

DEF intchk(name$,refname$)
chk=PING(name$,80)
if chk then 
  chk+=1
  goto final
else
  chk=PING(refname$,80)
endif
final:
return chk
end def
####### Living the colorful life #######

User avatar
Dutchman
Posts: 853
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Internet Check

Post by Dutchman » Thu Nov 22, 2018 9:35 am

Thank you Richard.
This website "What is the Best IP address to Ping to Test my Internet Connection?" provides useful additional information: https://etherealmind.com/what-is-the-be ... onnection/
We consider it a refreshing mental exercise to figure out ways to circumvent foolishness (Quote from Richard)

User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Internet Check

Post by rbytes » Thu Nov 22, 2018 12:20 pm

Thanks for this valuable information. :idea:
####### Living the colorful life #######

User avatar
Dutchman
Posts: 853
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Internet Check

Post by Dutchman » Fri Nov 23, 2018 6:37 pm

I have adapted the function for use in iConvert.
The necessary constants and error messages are now within the function. Moreover, if PING with code 80 fails, the test is again performed with code 8080, as George suggested.
As default 'refname$' I have chosen "dns.google.com".
It was mentioned in https://etherealmind.com/what-is-the-be ... onnection/
and explained further in https://developers.google.com/speed/pub ... over-https
It is the default name, but can be changed to 'url$' with the command: 'NetStatus.refname$=url$'

The message about the status is now also locally generated and returned as a result. Therefore, now the level range could also be changed to 0-2, because it does not have to fit in the command 'ON x GOTO'.
So level=0 means 'no internet'.
The result can now be printed in a single line of code. The level can be requested as a local variable.

Code: Select all

 
/*
NetStatus$() by Dutchman
adapted from "Internet Check" by rbytes, November 2018

To test this program:
a. Run it with name$ set to active websites. Return value = 2
b. Run it with name$ deliberately misspelled. Return value = 1
c. Run it with your device in Airplane mode. Return value = 0
*/

PRINT NetStatus$("www.apple.com")&" Statuslevel="&NetStatus$.level
END

'r'
DEF NetStatus$(name$)
' by Rbytes, adapted by Dutchman
' Return.level ranges from 0-2
' Usage:
' See https://bit.ly/2Aelgu7 and https://bit.ly/2PHqYzb
' On DNS: https://developers.google.com/speed/public-dns/docs/dns-over-https
DNSurl$="dns.google.com" 'See https://bit.ly/2PHqYzb
' refname$ can be preset to 'url$' with 'NetStatus.refname$=url$'
IF refname$="" THEN refname$=DNSurl$
'
ob=OPTION_BASE()
OPTION BASE 1
' Messages can be overwritten in NetStatus.Msg$()
IF Msg$(1)="" THEN GOSUB Init
'--- Ping with code=80 and code=8080
code=80
DO
  Level=0
  GOSUB Call
   IF code=80 AND Level<3 THEN
    FirstLevel=Level
    code=8080
  ENDIF
UNTIL Level=2 OR code=8080
Level=MAX(Level,FirstLevel)
Status$=Msg$(level+1)
OPTION BASE ob
RETURN Status$ ' reurn from function
'
'----- local subroutine
Call:
Level=PING(TRIM$(name$),code)
IF Level THEN 
  Level+=1
  RETURN
ELSE
  Level=PING(refname$,code)
ENDIF
RETURN ' from subroutine
'---- local subroutine
Init:
DIM Msg$(3)
FOR i=1 TO 3
  READ Msg$(i)
NEXT i
RETURN ' from local subroutine
DATA "Internet connection failed."
DATA "Website is not responding."
DATA "Website is responding."
END DEF
''
I also created a second version that retains the message as a local variable and returns the level.

Code: Select all

 /*
NetStatus() by Dutchman
adapted from "Internet Check" by rbytes, November 2018

To test this program:
a. Run it with name$ set to active websites. Return value = 2
b. Run it with name$ deliberately misspelled. Return value = 1
c. Run it with your device in Airplane mode. Return value = 0
*/

result=NetStatus("www.apple.com")
PRINT NetStatus.Status$& " Statuslevel="&result&". "
END

'r'
DEF NetStatus(name$)
' by Rbytes, adapted by Dutchman
' Return.level ranges from 0-2
' Usage:
' See https://bit.ly/2Aelgu7 and https://bit.ly/2PHqYzb
' On DNS: https://developers.google.com/speed/public-dns/docs/dns-over-https
DNSurl$="dns.google.com" 'See https://bit.ly/2PHqYzb
' refname$ can be preset to 'url$' with 'NetStatus.refname$=url$'
IF refname$="" THEN refname$=DNSurl$
'
ob=OPTION_BASE()
OPTION BASE 1
' Messages can be overwritten in NetStatus.Msg$()
IF Msg$(1)="" THEN GOSUB Init
'--- Ping with code=80 and code=8080
code=80
DO
  Level=0
  GOSUB Call
   IF code=80 AND Level<3 THEN
    FirstLevel=Level
    code=8080
  ENDIF
UNTIL Level=2 OR code=8080
Level=MAX(Level,FirstLevel)
Status$=Msg$(level+1)
OPTION BASE ob
RETURN Level ' reurn from function
'
'----- local subroutine
Call:
Level=PING(TRIM$(name$),code)
IF Level THEN 
  Level+=1
  RETURN
ELSE
  Level=PING(refname$,code)
ENDIF
RETURN ' from subroutine
'---- local subroutine
Init:
DIM Msg$(3)
FOR i=1 TO 3
  READ Msg$(i)
NEXT i
RETURN ' from local subroutine
DATA "Internet connection failed."
DATA "Website is not responding."
DATA "Website is responding."
END DEF
''
We consider it a refreshing mental exercise to figure out ways to circumvent foolishness (Quote from Richard)

User avatar
GeorgeMcGinn
Posts: 417
Joined: Sat Sep 10, 2016 6:37 am
My devices: IPad Pro
IMac
Location: Venice, FL
Flag: United States of America
Contact:

Re: Internet Check

Post by GeorgeMcGinn » Sun Dec 02, 2018 9:27 pm

Hi Dutchman,

I need to caution you about using an IP address for checking a specific website. The issue comes with whether or not the person who set up the webserver has set up one IP address for his machine, and uses the Domain Name Server (DNS) to resolve where the request goes.

You see this more with corporate websites then websites that are hosted by a company whose business is to host virtual websites. You see a corporation will have one IP address for all its Internet traffic and it will use the DNS to direct traffic to the proper page. When I had my webserver online, I hosted other people's websites, so to give them each their own IP address, I had apply and pay for a range of IP addresses. Otherwise I would have been stuck with my one IP address and had to use the DNS to resolve which subdirectory on the HTTPD server the request goes to.

That would be the only issue using just the IP address. It is useful, because if you can't get to the website and you know its IP address, then you can actually check to see if the entire machine is down or not. In other words, you could get a fail on the domain name but get a successful code using the IP address.

It may sound confusing but unfortunately that's the way the Internet is set up. There's only an infinite number of IP address is, and now that IoT devices will also have their own IP address, everybody's going to have to switch over to the IPv6 format. IoT are sensors and other devices like homemade home alarm systems that have Internet access to them will need to have an IP address and not a domain name. This means tens of billions of you more IP addresses will be needed in the future.

Wikipedia has a good write up on the IPv4 versus a IPv6, and why we will soon be running out of IP addresses. That's one of the reasons why virtual servers in virtual down domain names were created so that they all can use the same IP address it just gets dissolved by the server.

Link: https://en.m.wikipedia.org/wiki/IPv6


Dutchman wrote:
Thu Nov 22, 2018 9:35 am
Thank you Richard.
This website "What is the Best IP address to Ping to Test my Internet Connection?" provides useful additional information: https://etherealmind.com/what-is-the-be ... onnection/
George McGinn
Computer Scientist/Cosmologist/Writer/Photographer
Member: IEEE, IEEE Computer Society
IEEE Sensors Council & IoT Technical Community
American Association for the Advancement of Science (AAAS)
http://georgemcginn.wordpress.com
http://cosmologyandspace.wordpress.com

User avatar
Dutchman
Posts: 853
Joined: Mon May 06, 2013 9:21 am
My devices: iMac, iPad Air, iPhone
Location: Netherlands
Flag: Netherlands

Re: Internet Check

Post by Dutchman » Mon Dec 10, 2018 9:38 am

GeorgeMcGinn wrote:
Sun Dec 02, 2018 9:27 pm

I need to caution you about using an IP address for checking a specific website. The issue comes with whether or not the person who set up the webserver has set up one IP address for his machine, and uses the Domain Name Server (DNS) to resolve where the request goes.
Thank you, George for the reaction.
I only have some trouble to draw the conclusion. It is really confusing.
Can I use "dns.google.com" to check the network connection or do you recommend a different address?
Which address?
We consider it a refreshing mental exercise to figure out ways to circumvent foolishness (Quote from Richard)

User avatar
rbytes
Posts: 1798
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Internet Check

Post by rbytes » Mon Dec 10, 2018 3:36 pm

Therefore, now the level range could also be changed to 0-2, because it does not have to fit in the command 'ON x GOTO'.
I have used variables with values starting at 0 in ON x GOTO statements. It is true that ON x GOTO will treat 0 the same as 1. This is true regardless of the value of OPTION BASE x. But the following code shows a workaround:

Code: Select all

x=0
ON x+1 GOTO 10,20,30
10 BEEP!GOTO 40
20 BEEP!BEEP!GOTO 40
30 BEEP! BEEP!BEEP
40 PAUSE 5
END
It works with x having integer values from 0 to 2.
####### Living the colorful life #######

Post Reply