Test internet connection reliably?
Re: Test internet connection reliably? [Resolved]
All the InternetGetConnectedState API function tests for is the ability to have a connection, not the if the actual connection gets to the internet. If you have any type of recognized network interface path that is configured properly to work the API function will return true, it does not actually have to be connected to the internet.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably? [Resolved]
I know this is a really old post, but i had a small point to make here for this subject that came to mind while searching for something else.
Someone mentioned hard coding an address, and someone mentioned seeing an address via a hex dump and calling it spyware.
The API function InternetCheckConnection will always be true if the www url address will resolve to an IP address somewhere on the internet. For example, open a command prompt and ping http://www.mymachinesdasaxzy.com - it resolves to 207.223.0.140 - even though there is no such domain of http://www.mymachinesdasaxzy.com the API InternetCheckConnection returns true because the url resolves to an IP address somewhere on the internet (in this case to an IP address in a range of IP addresses held by Comcast). On the other hand, if you ping http://www.mymach_$inesdasaxzy.com you will find that it will not resolve to an IP address so the API function InternetCheckConnection will return false even though you may have a valid internet usage connection.
You can ping http://www.mymachinesdasaxzy.com and it will time out because there is no real domain to return a response to the ping (as there is no server for a http://www.mymachinesdasaxzy.com domain), even though the IP address resolved, but the timeout does not affect the API return as long as the url resolves to an IP address.
So, you can select nonsense or non-existant domains for use with the API InternetCheckConnection and if it will resolve to an IP address on the internet somewhere the API will return true which means that even with a nonsense or non-existant domain as long at it will resolve to an IP address on the internet the API InternetCheckConnection will indicate a valid connection to the internet as it is a valid connection to the internet because the resolved IP address does exist some place on the internet even if you get ping timeouts when trying to ping it. (that is of course if the non-existant or nonsense www address you used resolves to an IP address)
So in effect, the API InternetCheckConnection is not checking for a valid internet usage connection like its name imply's, its actually checking for a resolved IP address and assumes two things:
1. Failure of the url used to resolve to an IP address indicates that you do not have a valid usage connection to the internet (which can not always be true for the API InternetCheckConnection because you can have a vaild connection and a url not resolve to an IP address on the internet somewhere).
2. A url resolving to an IP address indicates that you do have a valid usage connection to the internet. (which will always be true for the API InternetCheckConnection because it will always return true if the url will resolve to an IP address somewhere on the internet)
Since you can have a vaild internet usage connection and the API InternetCheckConnection will return false if there is no resolved IP address from the url used with the API, it means that you can not trust the API to indicate the lack of a valid internet usage connection. However, because the API InternetCheckConnection will always return true if the url will resolve to an IP address on the internet somewhere you can trust the API to indicate the presence of a valid internet usage connection if the url will resolve to an IP address.
Maybe thinking about it as InternetCheckForResolvedAddress instead of InternetCheckConnection...
would help define this a little more, as in:
Information and code sample to illustrate this somewhat:

Someone mentioned hard coding an address, and someone mentioned seeing an address via a hex dump and calling it spyware.
The API function InternetCheckConnection will always be true if the www url address will resolve to an IP address somewhere on the internet. For example, open a command prompt and ping http://www.mymachinesdasaxzy.com - it resolves to 207.223.0.140 - even though there is no such domain of http://www.mymachinesdasaxzy.com the API InternetCheckConnection returns true because the url resolves to an IP address somewhere on the internet (in this case to an IP address in a range of IP addresses held by Comcast). On the other hand, if you ping http://www.mymach_$inesdasaxzy.com you will find that it will not resolve to an IP address so the API function InternetCheckConnection will return false even though you may have a valid internet usage connection.
You can ping http://www.mymachinesdasaxzy.com and it will time out because there is no real domain to return a response to the ping (as there is no server for a http://www.mymachinesdasaxzy.com domain), even though the IP address resolved, but the timeout does not affect the API return as long as the url resolves to an IP address.
So, you can select nonsense or non-existant domains for use with the API InternetCheckConnection and if it will resolve to an IP address on the internet somewhere the API will return true which means that even with a nonsense or non-existant domain as long at it will resolve to an IP address on the internet the API InternetCheckConnection will indicate a valid connection to the internet as it is a valid connection to the internet because the resolved IP address does exist some place on the internet even if you get ping timeouts when trying to ping it. (that is of course if the non-existant or nonsense www address you used resolves to an IP address)
So in effect, the API InternetCheckConnection is not checking for a valid internet usage connection like its name imply's, its actually checking for a resolved IP address and assumes two things:
1. Failure of the url used to resolve to an IP address indicates that you do not have a valid usage connection to the internet (which can not always be true for the API InternetCheckConnection because you can have a vaild connection and a url not resolve to an IP address on the internet somewhere).
2. A url resolving to an IP address indicates that you do have a valid usage connection to the internet. (which will always be true for the API InternetCheckConnection because it will always return true if the url will resolve to an IP address somewhere on the internet)
Since you can have a vaild internet usage connection and the API InternetCheckConnection will return false if there is no resolved IP address from the url used with the API, it means that you can not trust the API to indicate the lack of a valid internet usage connection. However, because the API InternetCheckConnection will always return true if the url will resolve to an IP address on the internet somewhere you can trust the API to indicate the presence of a valid internet usage connection if the url will resolve to an IP address.
Maybe thinking about it as InternetCheckForResolvedAddress instead of InternetCheckConnection...

Code: Select all
If InternetCheckForResolvedAddress(@url_x$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
......
;instead of
If InternetCheckConnection(@url_x$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
.....
Information and code sample to illustrate this somewhat:

Code: Select all
Procedure CheckInetConnect()
;note: InternetCheckConnection will always be true if the www address will resolve to an IP address
; for example, open a command prompt and ping www.mymachinesdasaxzy.com - it resolves to 207.223.0.140
; so InternetCheckConnection will return true even though there is no such domain of www.mymachinesdasaxzy.com
; this happens because that resolved IP address falls into the domain holdings of someone else. In the example
; using www.mymachinesdasaxzy.com - it resolves to 207.223.0.140 - it resolves to an IP address that is registered to Comcast
statusa.i
statusb.i
statusc.i
statusd.i
statusx.i
status.i
url_x$ = "http://www.mymachinesdasaxzy.com" ; resolves to an IP address in the comcast domain
url_a$="http://www.google.com"
url_b$="http://www.yahoo.com"
url_c$="http://www.google.de"
url_d$="http://www.dw-world.de"
If InternetCheckConnection_(@url_x$,#FLAG_ICC_FORCE_CONNECTION,0) = #False ; we pick our best guess for reliable connection check for the first check, if can't reach we try some others in case there is some weird internet thing going on - if this first one doesn't work out, if it does then we just skip the rest and go with our pick for the fastest and most reliable for indication.
If InternetCheckConnection_(@url_a$,#FLAG_ICC_FORCE_CONNECTION,0) = #True ; we got here because our best guess didn't return true
statusa = #True
Else
statusa = #False
EndIf
If statusa = #False ; whoops...can't get a connection to url_a$ so lets try this one
If InternetCheckConnection_(@url_b$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
statusb = #True
Else
statusb = #False
EndIf
EndIf
If statusb = #False ; whoops...can't get a connection to url_b$ but maybe we are in the other half of the world so lets try this one too
If InternetCheckConnection_(@url_c$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
statusc = #True
Else
statusc = #False
EndIf
EndIf
If statusc = #False ; whoops...can't get a connection to url_c$ so lets try it one more time to make sure
If InternetCheckConnection_(@url_d$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
statusd = #True
Else
statusd = #False
EndIf
EndIf
Else
statusx = #True
EndIf
; ok, we tried all the best ones from around the world, the four known to be good and reliable plus our best guess (which might be one from our ISP for example - one from the ISP would be the fastest), and...
; if any of them return true then we have a valid internet usage connection
If statusx = #True Or statusa = #True Or statusb = #True Or statusc = #True Or statusd = #True ; if any are true then we have a vaild usage connection for the internet.
ProcedureReturn #True
Else
ProcedureReturn #False ; and if none of them are true then its a pretty sure bet that we do not have a vaild internet usage connection at the time
EndIf
EndProcedure
Debug CheckInetConnect() ; if 1 then we have a valid internet usage connection, if 0 then we don't
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably?
Using ping to check for a connection? And its taking too long? Limit the number of echo requests to speed it up a little. Borrowing an example from this thread to illustrate:
notice the url$="www.google.com -n 1" < the added '-n 1' tells ping to only use one echo request and not the default four which takes longer .

Code: Select all
Procedure OnlineA(url$="www.google.com -n 1")
If InternetGetConnectedState_(0,0)=1 ; Only check further if modem is switched on.
p=RunProgram("ping.exe",url$,"",#PB_Program_Hide|#PB_Program_Open|#PB_Program_Read)
If p : While ProgramRunning(p) : o$+ReadProgramString(p) : Wend : CloseProgram(p) : EndIf
If o$<>"" And FindString(o$,"unreachable",1)=0 And FindString(o$,"could not find host",1)=0 : status=1 : EndIf
EndIf
ProcedureReturn status
EndProcedure
Debug OnlineA()

The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably?
Hi SFSxOI Thanks for a very fine in depth explanation, realy great.
But you should always assume that the return value is in the user's own language
when it succeeds
When Blocked connection
With Unknown Domain
I don't what would be best here, but at least the return string is very long when it succeeds, maybe you can use that ?
I realy appreciate your effort here.
Best Henrik

But you should always assume that the return value is in the user's own language
In my case:...
If p : While ProgramRunning(p) : o$+ReadProgramString(p) : Wend : CloseProgram(p) : EndIf
If o$<>"" And FindString(o$,"unreachable",1)=0 And FindString(o$,"could not find host",1)=0 : status=1 : EndIf
...
when it succeeds
Code: Select all
Pinger www.l.google.com [209.85.149.99] med 32 byte data:
Svar fra 209.85.149.99: byte=32 tid=50ms TTL=56
Ping-statistikker for 209.85.149.99:
Pakker: Sendt = 1, modtaget = 1, tabt = 0 (0% tab),
Beregnet tid for rundtur i millisekunder:
Minimum = 50ms, Maksimum = 50ms, Gennemsnitlig = 50ms
Return 1 for status
Code: Select all
Ping-anmodning kunne ikke finde v‘rten www.google.com. Kontroller navnet, og pr›v igen.
Returns 1 for status
With Unknown Domain
Code: Select all
Ping-anmodning kunne ikke finde v‘rten www.jhrnbfyerbxdlsBoogle.com. Kontroller navnet, og pr›v igen.
Returns 1 for status
I don't what would be best here, but at least the return string is very long when it succeeds, maybe you can use that ?
I realy appreciate your effort here.

Best Henrik
Re: Test internet connection reliably?
Thanks Henrik 
Well, the ping code was not mine, I borrowed it from this thread to use as an example because the original poster (UserOfPure) of the code in this thread posted it just like that, all i did was add the switch, and since i'm in the english forum I assume that posting in english is OK which is good for me because I don't really speak any other language and i'm sure other language speakers would appreciate not seeing me butcher their language by trying to use a translator or come up with what little I might know of another language
so by posting in an english speaking forum I really don't assume what the returns will be for another language.
If you use an unknown domain or a non-sense domain url with the API InternetCheckConnection, it has to resolve to an actual IP address used on the internet. The example I used (http://www.mymachinesdasaxzy.com) does resolve to an actual IP address even though its a non-existant domain url.
I don't use a ping for determining a valid connection. Instead I use the API InternetCheckConnection for a few different reasons but one is it gets rid of that pesky 'language' thing for ping like in the code above in case I do give out some code to someone who uses another language. The API InternetCheckConnection is "language agnostic" as it only cares if a URL resolved to an IP address or not and if it does it returns true which is a 1 which is probably still a 1 in most languages (I think), but you get the point.
For the ping thing; If o$ is not empty and the words/phrases "unreachable" and "could not find host" are not in the string then the ping is assumed to have suceeded. You don't need the string for other than condition testing to see if the ping suceeded. Of course, you will need to make the necessary language adjustments for your own language if you choose to use the ping method.
Not sure what you mean by a 'blocked connection' but if its blocked as in can't be used to access the internet beyond your ISP or network domains, then its not a usable internet connection.
If your concerned also about detecting a blocked connection (for purposes of browsing on the internet and general usage) you could also add checks for other protocols and things like ICMP (I think someone mentioned using ICMP in this thread somewhere) or IPv6 (for ip6 eg... 'ping ipv6.google.com -n 1 -6' for an IPv6 ping) or even getting well known web page strings (not the whole page) from well known urls like http://www.yahoo.com.
if your concerned that a target IP address for a ping check might be blocked to returns, the API InternetCheckConnection solves that problem too as it does not care if the IP returns anything, only if the url used with the API will resolve to an IP address.
I'd go with the API InternetCheckConnection and dispense with the ping thing. All you would need from the ping thing anyway is a simple yes or no for a connection check, and code for using, with filtering for specific words and conditions, it is much longer then a simple one liner like which will do the same thing but, in my opinion, more reliably for indicating a valid connection (as long as the url used will resolve to an Ip address used somewhere on the internet).

Well, the ping code was not mine, I borrowed it from this thread to use as an example because the original poster (UserOfPure) of the code in this thread posted it just like that, all i did was add the switch, and since i'm in the english forum I assume that posting in english is OK which is good for me because I don't really speak any other language and i'm sure other language speakers would appreciate not seeing me butcher their language by trying to use a translator or come up with what little I might know of another language

If you use an unknown domain or a non-sense domain url with the API InternetCheckConnection, it has to resolve to an actual IP address used on the internet. The example I used (http://www.mymachinesdasaxzy.com) does resolve to an actual IP address even though its a non-existant domain url.
I don't use a ping for determining a valid connection. Instead I use the API InternetCheckConnection for a few different reasons but one is it gets rid of that pesky 'language' thing for ping like in the code above in case I do give out some code to someone who uses another language. The API InternetCheckConnection is "language agnostic" as it only cares if a URL resolved to an IP address or not and if it does it returns true which is a 1 which is probably still a 1 in most languages (I think), but you get the point.
For the ping thing; If o$ is not empty and the words/phrases "unreachable" and "could not find host" are not in the string then the ping is assumed to have suceeded. You don't need the string for other than condition testing to see if the ping suceeded. Of course, you will need to make the necessary language adjustments for your own language if you choose to use the ping method.
Not sure what you mean by a 'blocked connection' but if its blocked as in can't be used to access the internet beyond your ISP or network domains, then its not a usable internet connection.
If your concerned also about detecting a blocked connection (for purposes of browsing on the internet and general usage) you could also add checks for other protocols and things like ICMP (I think someone mentioned using ICMP in this thread somewhere) or IPv6 (for ip6 eg... 'ping ipv6.google.com -n 1 -6' for an IPv6 ping) or even getting well known web page strings (not the whole page) from well known urls like http://www.yahoo.com.
if your concerned that a target IP address for a ping check might be blocked to returns, the API InternetCheckConnection solves that problem too as it does not care if the IP returns anything, only if the url used with the API will resolve to an IP address.
I'd go with the API InternetCheckConnection and dispense with the ping thing. All you would need from the ping thing anyway is a simple yes or no for a connection check, and code for using, with filtering for specific words and conditions, it is much longer then a simple one liner like
Code: Select all
If InternetCheckConnection_(@url_x$,#FLAG_ICC_FORCE_CONNECTION,0) = #True
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably?
hi i test the reliability of my internet connection from here http://www.whoisxy.com/
my results are
Pinging 192.168.0.3 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Reply from 208.109.112.157: TTL expired in transit.
Ping statistics for 192.168.0.3:
Packets: Sent = 4, Received = 1, Lost = 3 (75% loss),
my results are
Pinging 192.168.0.3 with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Reply from 208.109.112.157: TTL expired in transit.
Ping statistics for 192.168.0.3:
Packets: Sent = 4, Received = 1, Lost = 3 (75% loss),
Re: Test internet connection reliably?
FYI
This is how Windows 7 does it. http://blog.superuser.com/2011/05/16/wi ... awareness/
Very interesting article.
how could we employ similar approach in purebasic
This is how Windows 7 does it. http://blog.superuser.com/2011/05/16/wi ... awareness/
Very interesting article.
how could we employ similar approach in purebasic
Thanks,
ViiArtz
ViiArtz
Re: Test internet connection reliably?
Just convert the samples shown here > http://msdn.microsoft.com/en-us/library ... S.85).aspxviiartz wrote:FYI
This is how Windows 7 does it. http://blog.superuser.com/2011/05/16/wi ... awareness/
Very interesting article.
how could we employ similar approach in purebasic
get the MS SDK download and look up the interfaces
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably?
Link is broken!!
Just convert the samples shown here > http://msdn.microsoft.com/en-us/library ... S.85).aspx
get the MS SDK download and look up the interfaces
Thanks,
ViiArtz
ViiArtz
Re: Test internet connection reliably?
copy and paste it. its not broken, I was just looking at that page, sometimes the forum doesn't do links too well.
Oh, by the way, yes thats how windows 7 checks for connectivity, but using the interface method outlined in the MSDN is really overly complicated just to check to see if your connected. The method i outlined above using the one liner API 'InternetCheckConnection' thing is simple, works reliably (as described above if the address will resolve - choose something well known like www.yahoo.com or something) and takes only less than a minute to whip up some simple code, and does the same thing - checks for an internet connection.
Oh, by the way, yes thats how windows 7 checks for connectivity, but using the interface method outlined in the MSDN is really overly complicated just to check to see if your connected. The method i outlined above using the one liner API 'InternetCheckConnection' thing is simple, works reliably (as described above if the address will resolve - choose something well known like www.yahoo.com or something) and takes only less than a minute to whip up some simple code, and does the same thing - checks for an internet connection.
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
Re: Test internet connection reliably?
Na! copied and pasted... still not finding a page!copy and paste it. its not broken, I was just looking at that page, sometimes the forum doesn't do links too well.
Page Not Found
We're sorry, but the page you requested could not be found. Please check your typing and try again, or use the search options on this page.
Thanks,
ViiArtz
ViiArtz
-
- Enthusiast
- Posts: 443
- Joined: Sun Apr 06, 2008 12:54 pm
- Location: Brisbane, Qld, Australia
- Contact:
Re: Test internet connection reliably?
I think the Forum software (phpBB) is messing up the link. Try copy and paste from here:
Code: Select all
http://msdn.microsoft.com/en-us/library/ee264321(v=VS.85).aspx
-
- Addict
- Posts: 4777
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: Test internet connection reliably?
That's old news.ozzie wrote:I think the Forum software (phpBB) is messing up the link.

The links just have to be posted properly, see e.g. http://www.purebasic.fr/english/viewtopic.php?t=33728
Re: Test internet connection reliably?
hmmm, once again then > http://msdn.microsoft.com/en-us/library ... S.85).aspx
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.