Sample code for 30+ languages & platforms
DataFlex

Check Internet Connectivity

See more Socket/SSL/TLS Examples

Demonstrates an efficient way to test for Internet connectivity.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSocket
    String sTargetIp
    Integer iPort
    Boolean iSsl
    Integer iTimeoutMs
    String sTemp1

    Move False To iSuccess

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End
    // 1. Use a reliable Anycast IP. 
    //    8.8.8.8 (Google) or 1.1.1.1 (Cloudflare) are the industry standards.
    Move "8.8.8.8" To sTargetIp

    // 2. Use Port 53 (DNS). 
    //    DNS servers typically listen on TCP Port 53 as well as UDP. 
    //    (Alternatively, use port 443 if you suspect port 53 is blocked).
    Move 53 To iPort

    // 3. Disable SSL (0). 
    //    We are not doing a handshake, just a TCP connection.
    Move False To iSsl

    // 4. Short Timeout (1500ms).
    //    If you can't reach Google in 1.5 seconds, the connection is 
    //    likely too poor for practical use anyway.
    Move 1500 To iTimeoutMs

    // Connect
    Get ComConnect Of hoSocket sTargetIp iPort iSsl iTimeoutMs To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSocket To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComClose Of hoSocket 10 To iSuccess

    Showln "We have Internet connectivity."


End_Procedure