Check available connection with tsNet?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 8:24 am

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.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Check available connection with tsNet?

Post by mrcoollion » Tue Mar 31, 2020 9:05 am

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

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 9:14 am

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.

bangkok
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 937
Joined: Fri Aug 15, 2008 7:15 am

Re: Check available connection with tsNet?

Post by bangkok » Tue Mar 31, 2020 9:23 am


mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Check available connection with tsNet?

Post by mrcoollion » Tue Mar 31, 2020 9:43 am

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,

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 10:04 am

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

Code: Select all

tsNetClose
tsNetInit
before sending tsNetHeadSync command.
This is quite annoying because there is a risk to interrupt another asynchronous connection.

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Check available connection with tsNet?

Post by mrcoollion » Tue Mar 31, 2020 10:20 am

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
========

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 10:27 am

This script works fine, but I have to wait about 15 seconds to have an answer if I'm disconnected.

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » 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() ?

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 -
aPic_testConnection.png
Is you is, or is you ain't....
aPic_testConnection.png (10.4 KiB) Viewed 7477 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 :?
Image

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 11:59 am

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

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Check available connection with tsNet?

Post by AxWald » 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:

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!
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!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Tue Mar 31, 2020 12:47 pm

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 :twisted:
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 :P
Image

Zax
Posts: 469
Joined: Mon May 28, 2007 10:12 am
Location: France
Contact:

Re: Check available connection with tsNet?

Post by Zax » Tue Mar 31, 2020 1:06 pm

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 :P
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.

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Check available connection with tsNet?

Post by AxWald » Tue Mar 31, 2020 2:19 pm

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! ;-)
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!

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Check available connection with tsNet?

Post by bogs » Tue Mar 31, 2020 2:45 pm

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 :P
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”