Page 1 of 2
Check available connection with tsNet?
Posted: Tue Mar 31, 2020 8:24 am
by Zax
Hello,
Using LC Indy 9.04, I would like to test if an internet connection is available from a stack.
What is the best way to perform such a test with tsNet?
I tried the following test with an ethernet cable and without wifi:
1 - ethernet cable connected: recovery of headers from google.com with tsNetHeadSync() --> OK
2 - cable disconnected: same request with tsNetHeadSync() --> wait about 10 seconds to have the expected error tsneterr ("unable to resolve DNS")
3 - second attempt --> the error is immediate, probably because the previous URL was cached, and I don't know how to clear the cache with tsnet
If I do the same test with a browser (Firefox for example), the return is immediate and not cached. How does the browser know immediately if there is a connection available, when I have to wait about 10 seconds with tsNet?
I suppose I could try with tsNetHeadSync() with very short timeOuts, but it doesn't seem to be a safe test on low bandwidth connection.
Thank you.
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 9:05 am
by mrcoollion
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 9:14 am
by Zax
mrcoollion wrote: ↑Tue Mar 31, 2020 9:05 am
Maybe this will help.
Thank you for your reply Paul, but I read these documentations from two days, and it doesn't really help me... Unless I miss an important info.
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 9:23 am
by bangkok
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 9:43 am
by mrcoollion
maybe this could help you
http://runtime-revolution.278305.n4.nab ... 26361.html
see the script code at : Paul McClernan Jul 27, 2018; 7:48pmRe: Checking if internet is connected using tsNet?
Regards,
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 10:04 am
by Zax
Very interesting links, thanks
Meanwhile, I tried to play with tsNetSetTimeouts command, without great success. The only way to have a
quick answer from server (OK, you're connected, or can't resolve DNS) would be to insert
before sending
tsNetHeadSync command.
This is quite annoying because there is a risk to interrupt another asynchronous connection.
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 10:20 am
by mrcoollion
This works for me.
Just put it into a button script
Code: Select all
on mouseup
put "www.google.com" into pSelectedURL
put checkURLAvailable (pSelectedURL) into tConnectionYN
answer tConnectionYN
end mouseup
=========
function checkURLAvailable pSelectedURL
put tsNetHeadSync(pSelectedURL, tSentHeaders, tResult, tBytes) into tHeaders # into tHeaders
## get url pSelectedURL
if (tResult < 400) then
return "Ok"
else
return "Offline or Low Bandwidth or not a valid url or url not accessible!"
end if
end checkURLAvailable
========
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 10:27 am
by Zax
This script works fine, but I have to wait about 15 seconds to have an answer if I'm disconnected.
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 11:01 am
by bogs
This is more of a curiousity question than an answer to your post, but to check whether a connection is available or not, why would you use tsNet at all, instead of say, shell() ?
For instance, on 'nix, if I want to know if a site is up, I'd do something like (psuedo code) -
Code: Select all
# where ever you are testing from...
put shell("ping www.google.com -c1") into tConn
if "1 received" is not among the words of tConn then answer "You have no connection"
# continue your code...
The following is tested (on Linux, you may need to modify for Mac or Win) and working code as an example. Here is the look of the stack -

- Is you is, or is you ain't....
- aPic_testConnection.png (10.4 KiB) Viewed 9383 times
...and the code of the button....
Code: Select all
on mouseUp
put shell("ping " & field 1 &" -c1") into field 2
if matchText( field 2,"1 received") is false then answer "No Connection"
end mouseUp
Enter a correct address, such as "
www.google.com", and you should see nothing, other than the results in field 2.
Enter a made up address, which you know won't connect, and the answer dialog appears.
This is so simple, I am sure there must be a reason it won't work as a test

Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 11:59 am
by Zax
Interesting solution, bogs.
MacOS version of your script:
Code: Select all
on mouseUp
put shell("ping " & field 1 &" -c1") into field 2
if matchText( field 2,"1 packets received") is false then answer "No Connection" else answer "You are connected."
end mouseUp
Unfortunately, some websites don't answer pings. So your script will not work to test a connection to a specific website.
Anyhow, it works fine with
google.com, so your script is good to test general connection (and it was the purpose, so thank you very much

)
This is a version that returns the ping delay, or 0 if no connection:
Code: Select all
function getPingDelay
// return 0 if not available connection
put "ping google.com -c1 | sed '$!d;s|.*/\([0-9.]*\)/.*|\1|'" into cmd
get shell(cmd)
if (it is not a number) then
return 0 // instead of an error string
else return it
end getPingDelay
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 12:33 pm
by AxWald
Hi,
bogs wrote: ↑Tue Mar 31, 2020 11:01 am
This is more of a curiousity question than an answer to your post, but to check whether a connection is available or not, why would you use tsNet at all, instead of say, shell() ?
it's even more easy:
Code: Select all
function INTest withTime
-- put "myDomain.org/myip.php" into gName_IPURL (if you don't have it in a global)
if withTime then -- return IP & latency
put the millisecs into t1
put url gName_IPURL into myIP
put the millisecs - t1 into t2
return myIP & CR & t2
else -- return IP only
return url gName_IPURL
end if
end INTest
For sure, this requires an URL you can try to connect to. But usually you want to connect to something specific, so this way you can check if it's on line, additionally. And if "withTime" = true, you'll even get the latency!
You'll notice I call a PHP script. That's because I have a tiny "myip.php" file on all of my servers:
Code: Select all
<?PHP
function getUserIP()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
return $ip;
}
$user_ip = getUserIP();
echo $user_ip; // Output IP address [Ex: 177.87.193.134]
?>
This returns your public IP address (if you have IN). Comes handy in quite some cases :) So, with a few lines of code and a tiny PHP you get:
- Information if you have IN
- Information if your server is up
- Information if your servers PHP is running
- Latency to your server
- and your public IP.
Without touching tsNet at all - you
don't even need the internet inclusion!
Have fun!
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 12:47 pm
by bogs
AxWald wrote: ↑Tue Mar 31, 2020 12:33 pm
Hi,
bogs wrote: ↑Tue Mar 31, 2020 11:01 am
This is more of a curiousity question than an answer to your post, but to check whether a connection is available or not, why would you use tsNet at all, instead of say, shell() ?
it's even more easy.
While I think that is danged spiffy, AxWald, I think your definition of "easy" and mine are very much different
Zax wrote: ↑Tue Mar 31, 2020 11:59 am
Unfortunately, some websites don't answer pings.
No, but all websites that are reachable return 'something', and you can test for that, the test doesn't have to be what I wrote up there, that was just a q&d example

Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 1:06 pm
by Zax
bogs wrote: ↑Tue Mar 31, 2020 12:47 pm
Zax wrote: ↑Tue Mar 31, 2020 11:59 am
Unfortunately, some websites don't answer pings.
No, but all websites that are reachable return 'something', and you can test for that, the test doesn't have to be what I wrote up there, that was just a q&d example
Well, I tested some websites with your shell script, and for some of them I had something like "1 packet send, 0 returned". In this case, it's difficult to say if the website is up.
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 2:19 pm
by AxWald
Hi,
bogs wrote: ↑Tue Mar 31, 2020 12:47 pm
[...] I think your definition of "easy" and mine are very much different [...]
Don't let my verbosity bedazzle you - basically it's:
Code: Select all
if URL aKnownGoodURL is empty then youAreOffLine
Have fun! ;-)
Re: Check available connection with tsNet?
Posted: Tue Mar 31, 2020 2:45 pm
by bogs
Zax wrote: ↑Tue Mar 31, 2020 1:06 pm
Well, I tested some websites with your shell script, and for some of them I had something like "1 packet send, 0 returned". In this case, it's difficult to say if the website is up.
Heh, Couldn't you give me a hint ? LOL.
Please copy/paste field 2's result, or give me an example address, cause without anything to go on, I can't possibly improve the solution for you
