Detecting active ethernet connection

Bringing the internet highway into your project? Building FTP, HTTP, email, chat or other client solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Mjhants
Posts: 19
Joined: Wed Jan 14, 2015 11:47 pm

Detecting active ethernet connection

Post by Mjhants » Thu May 28, 2015 11:42 am

Have an OSX and Windows application written in Livecode that is happily communicating over TCP/IP with another system and talking happily with it. All good!

This system can be on a local network with no external internet access or could be on the other side of the world, connected over the internet.

The application can sit doing nothing for several minutes and then issue a command. In an ideal world this all works well but want to add some code to check that the end user system is still there. For example if I disconnect the ethernet cable I want to capture that.

On our development OSX system the mac knows (almost) instantly that there is no ethernet connection - I cannot find a way of detecting this in Livecode, any suggestions?

One option is use ping but this is not supported directly by Livecodes (limited) socket support and the external call seems to take several seconds and tie the system up while its doing it.

So any suggestions from the community?

Thanks in advance!


Mark

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Detecting active ethernet connection

Post by Martin Koob » Thu May 28, 2015 2:49 pm

Hi.

On the Mac you can using a couple of shell commands you can check whether a network port has an IP address.
networksetup -listallnetworkservices --lists all available services
networksetup -getInfo [service name] --gets info on that service including whether it has an IP address.

Code: Select all

function checkForNetworkConnection
     put shell("networksetup -listallnetworkservices") into tPorts
   repeat for each line tPort in tPorts
      If tport contains "*" then next repeat -- skips first line of shell output and any disabled services
      put shell("networksetup -getInfo" && quote & tport & quote) into tPortInfo
      filter  lines of tPortInfo with "IP Address*"
      if tPortInfo is not empty then
         return tPort && tPortInfo
      end if
   end repeat
   return "No Network Connection"
end checkForNetworkConnection
I tested this by turning my Wifi on and off and it showed whether i was connected or not. Not sure if it would work in all cases.

I don't know what the Windows or Linux versions of these shell commands are.

Martin

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: Detecting active ethernet connection

Post by Martin Koob » Thu May 28, 2015 4:26 pm

Hi

Just realized you could have two or more active services so I re-wrote the function to return a list of any services that have an IP address or return an error if there are none. This is assuming if a service has an IP address it is connected to a network. I think this is a safe assumption but perhaps someone knows otherwise.

Code: Select all

function checkNetworkConnection
     put shell("networksetup -listallnetworkservices") into tServices
   repeat for each line tService in tServices
      If tService contains "*" then next repeat
      put shell("networksetup -getInfo" && quote & tService & quote) into tServiceInfo
      filter  lines of tServiceInfo with "IP Address*"
      if tServiceInfo is not empty then
         put tService && tServiceInfo & CR after tActiveServices
      end if
   end repeat
   if tActiveServices is not empty then
      delete the last character of tActiveServices
      return tActiveServices
   else
      return "Error: No Network Connection"
      end if
end checkNetworkConnection
This won't alert you if connection status of one of the services changes of course. You would have to periodically call this function.

Martin

Mjhants
Posts: 19
Joined: Wed Jan 14, 2015 11:47 pm

Re: Detecting active ethernet connection

Post by Mjhants » Thu May 28, 2015 11:12 pm

Thanks Martin that is an interesting approach. Will give it a try and let you know how I get on.


Mark

marco.deepak
Posts: 11
Joined: Fri Oct 02, 2020 10:24 am

Re: Detecting active ethernet connection

Post by marco.deepak » Mon Oct 12, 2020 10:14 am

Any Windows based solution to the above question?

Thanks

Post Reply

Return to “Internet”