KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Check If Internet Connection is Active
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: August 1, 2023

Below is a utility method that takes advantage of the new Transporter Classes .checkConnection() function:

// Method: Util_IsNetActive
//
// Description: Uses SMTP Transporter Class's .checkConnection() function to check if internet connection is active
//
// Returns a Boolean:
// - True : There is an active internet connection
// - False : There is not an active internect connection

#DECLARE()->$connected_b : Boolean

$connected_b:=False

var $hostPrimary_t : Text
var $hostRedundancy_c : Collection
var $options_o; $status_o : Object
var $transporter : 4D.SMTPTransporter
var $statusPrimary_b : Boolean

// Assign a Primary SMTP Server to check against
$hostPrimary_t:="smtp.gmail.com"

// Assign any number of SMTP Servers to the collection as a Redundancy Check if the Primary Server is down
$hostRedundancy_c:=["smtp.mail.yahoo.com"; "email-smtp.us-west-1.amazonaws.com"]


$options_o:=New object("host"; $hostPrimary_t)
$transporter:=SMTP New transporter($options_o)
$status_o:=$transporter.checkConnection()

If ($status_o.success=True)
   $connected_b:=True
Else
  
   // Just in case the Primary SMTP server is down, perform a second check against one of the additional SMTP servers randomly
   $options_o:=New object("host"; $hostRedundancy_c[Mod(Random; $hostRedundancy_c.length)])
   $transporter:=SMTP New transporter($options_o)
   $status_o:=$transporter.checkConnection()
  
   $connected_b:=$status_o.success
End if


The method can simply be called and will return a True an active internet connection is available or a False if not, assuming that there is nothing preventing an SMTP connection.

The other transporter classes can also be used:
4D.POP3Transporter
4D.IMAPTransporter
These both also have a .checkConnection() function.

The Primary and Redundancy hosts can be modified to another host of choice.