Check available connection with tsNet?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Check available connection with tsNet?
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.
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.
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Check available connection with tsNet?
Here is some tsNet information. Maybe this will help.
Regards,
Paul
https://www.techstrategies.com.au/tsnet ... -external/
https://www.techstrategies.com.au/tsnet-resources/
https://www.techstrategies.com.au/tsnet-command-list/
You probably could use use tsNetSetTimeouts (set timeouts) or tsNetGetTimeouts (see what the timeouts are) to prevent long waiting times.
https://www.techstrategies.com.au/tsnet ... etTimeouts
https://www.techstrategies.com.au/tsnet ... etTimeouts
Regards,
Paul
https://www.techstrategies.com.au/tsnet ... -external/
https://www.techstrategies.com.au/tsnet-resources/
https://www.techstrategies.com.au/tsnet-command-list/
You probably could use use tsNetSetTimeouts (set timeouts) or tsNetGetTimeouts (see what the timeouts are) to prevent long waiting times.
https://www.techstrategies.com.au/tsnet ... etTimeouts
https://www.techstrategies.com.au/tsnet ... etTimeouts
Re: Check available connection with tsNet?
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?
One original solution here :
http://lists.runrev.com/pipermail/use-l ... 59073.html
Or one classic there :
viewtopic.php?f=53&t=33635
http://lists.runrev.com/pipermail/use-l ... 59073.html
Or one classic there :
viewtopic.php?f=53&t=33635
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Check available connection with tsNet?
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,
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?
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.
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
Code: Select all
tsNetClose
tsNetInit
This is quite annoying because there is a risk to interrupt another asynchronous connection.
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Check available connection with tsNet?
This works for me.
Just put it into a button script
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?
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?
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) -
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 -
...and the code of the button....
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
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...
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 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?
Interesting solution, bogs.
MacOS version of your script:
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:
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
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?
Hi,
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:
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:
Have fun!
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
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]
?>
- 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.
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: Check available connection with tsNet?
While I think that is danged spiffy, AxWald, I think your definition of "easy" and mine are very much different
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?
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?
Hi,
Have fun! ;-)
Don't let my verbosity bedazzle you - basically it's:
Code: Select all
if URL aKnownGoodURL is empty then youAreOffLine
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!
Re: Check available connection with tsNet?
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