Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) Socket Connect Failure Cases

Demonstrates a few common situations that would cause a TCP connect to a remote host:port to fail, an examines the LastErrorText for each case.

This example discusses the following cases:

  1. The remote host is not listening on the desired port.
  2. No remote host exists at the IP address (such as on a LAN).
  3. The local Windows Firewall blocks the outbound connection.
  4. Your ISP blocks the outbound connection.
  5. The remote Windows Firewall blocks the inbound connection.
  6. The domain cannot be resolved to an IP address.
Chilkat ran these experiments on a Windows computer using Chilkat v9.5.0.51. The error messages may differ depending on the operating system. A connection failure can be immediate or after a timeout. In both cases, there can be numerous causes for the same error messages and behavior. Diagnosing a failure-to-connect problem involves understanding what potential blocking infrastructure (hardware and/or software) can exist between the program initiating the connection on the local computer, and the program accepting the connection at the remote host:port.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoSocket
    Boolean iTls
    Integer iMaxWaitMillisec
    Boolean iSuccess
    String sTemp1

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatSocket)) To hoSocket
    If (Not(IsComObjectCreated(hoSocket))) Begin
        Send CreateComObject of hoSocket
    End

    Move False To iTls
    Move 5000 To iMaxWaitMillisec

    // First test the case where the remote host is not listening on the desired port.
    // In this case, my LAN has a computer at 192.168.1.137, but nothing on that computer
    // is listening for inbound connection to port 6660.
    Get ComConnect Of hoSocket "192.168.1.137" 6660 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    // The call to Connect should fail quickly, and the LastErrorText will contain
    // a message about the connection being rejected:
    // 
    //      socket2Connect:
    //        connect2:
    //          ConnectFailReason: Connection rejected
    //        --connect2
    //      --socket2Connect

    // Now test the case where there is no computer on the network at the desired IP address.
    // For example, on my LAN, there is no computer at 192.168.1.188.  If ping is used to test,
    // the error message would be "Destination host unreachable".

    Get ComConnect Of hoSocket "192.168.1.188" 80 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    // In this case, the connection is "rejected" as before, but the connect hangs for the entire
    // maximum waiting period (maxWaitMillisec), which is 5 seconds in this case.
    // (If the maxWaitMillisec is a large enough amount of time, the operating system
    // may fail the connect with a "connection rejected" after a long wait. 
    // The difference is that when the remote host exists and is reachable, the failure is quick.)
    // The portion of the LastErrorText for this failure is shown here:

    //    connectInner:
    //      hostname: 192.168.1.188
    //      port: 80
    //      tls: 0
    //      maxWaitMs: 5000
    //      socket2Connect:
    //        connect2:
    //          connectSocket:
    //            connect_ipv6_or_ipv4:
    //              timeout waiting for connect to complete
    //              numSec: 5
    //              numMicroSec: 0
    //              failedWaitToConnect: Socket operation timeout.
    //            --connect_ipv6_or_ipv4
    //          --connectSocket
    //          ConnectFailReason: Timeout
    //        --connect2
    //      --socket2Connect
    //      Failed.
    //    --connectInner

    // Imagine that a new rule is added to Windows Firewall to block all connections
    // for this example app.  The characteristics of the failure are the same as for
    // the case where nothing on the remote computer is listening at the given port.
    // The connection attempt fails immediately, and the error message (from the OS) is
    // "connection rejected".  Also notice that SSL/TLS never enters the equation because
    // the SSL/TLS handshake begins *after* the TCP connection is completed.  When the 
    // TCP connection fails, it never reaches the point of even beginning the SSL/TLS negotiation.
    Move True To iTls
    Get ComConnect Of hoSocket "192.168.1.188" 80 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    //    connectInner:
    //      hostname: smtp.gmail.com
    //      port: 465
    //      tls: 1
    //      maxWaitMs: 5000
    //      socket2Connect:
    //        connect2:
    //          ConnectFailReason: Connection rejected
    //        --connect2
    //      --socket2Connect
    //      Failed.
    //    --connectInner

    // What about the case where the ISP blocks an outbound connection?  My home ISP is 
    // Comcast cable, and they block outbound SMTP port 25.  
    Move False To iTls
    Get ComConnect Of hoSocket "pop3.btconnect.com" 25 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    // The result is a timeout:
    //    connectInner:
    //      hostname: pop3.btconnect.com
    //      port: 25
    //      tls: 0
    //      maxWaitMs: 5000
    //      socket2Connect:
    //        connect2:
    //          connectSocket:
    //            connect_ipv6_or_ipv4:
    //              timeout waiting for connect to complete
    //              numSec: 5
    //              numMicroSec: 0
    //              failedWaitToConnect: Socket operation timeout.
    //            --connect_ipv6_or_ipv4
    //          --connectSocket
    //          ConnectFailReason: Timeout
    //        --connect2
    //      --socket2Connect
    //      Failed.
    //    --connectInner

    // What about the case where the Windows Firewall at the remote host is blocking the
    // inbound connection.  On Chilkat's internal LAN, there is a computer at 192.168.1.127
    // running an SSH server.  After adding an inbound rule to block port 22, external connections
    // fail with a timeout, as shown below:

    Get ComConnect Of hoSocket "192.168.1.127" 22 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    //    connectInner:
    //      hostname: 192.168.1.127
    //      port: 22
    //      tls: 0
    //      maxWaitMs: 5000
    //      socket2Connect:
    //        connect2:
    //          connectSocket:
    //            connect_ipv6_or_ipv4:
    //              timeout waiting for connect to complete
    //              numSec: 5
    //              numMicroSec: 0
    //              failedWaitToConnect: Socket operation timeout.
    //            --connect_ipv6_or_ipv4
    //          --connectSocket
    //          ConnectFailReason: Timeout
    //        --connect2
    //      --socket2Connect
    //      Failed.
    //    --connectInner

    // Finally, what if we try to connect to a hostname that does not resolve to an IP address?

    Get ComConnect Of hoSocket "www.thisdoesnotresolvetoanyipaddressxyz.com" 22 iTls iMaxWaitMillisec To iSuccess
    Get ComLastErrorText Of hoSocket To sTemp1
    Showln sTemp1

    // In this case there is an immediate failure:
    //    connectInner:
    //      hostname: www.thisdoesnotresolvetoanyipaddressxyz.com
    //      port: 22
    //      tls: 0
    //      maxWaitMs: 5000
    //      socket2Connect:
    //        connect2:
    //          connectSocket:
    //            connect_ipv6_or_ipv4:
    //              resolveHostname6:
    //                getAddressInfo:
    //                  Failed to get host address info. (3)
    //                  SocketError: WSAHOST_NOT_FOUND No such host is known.
    //                  hostOrIpAddr: www.thisdoesnotresolvetoanyipaddressxyz.com
    //                  port: 22
    //                  Versions of Windows earlier than Windows XP are limited to handling IPv4 only
    //                  On Windows Server 2003 and Windows XP, IPv6 addresses are returned only if IPv6 is installed on the local computer.
    //                --getAddressInfo
    //              --resolveHostname6
    //              Domain to IP address resolution failed.
    //            --connect_ipv6_or_ipv4
    //          --connectSocket
    //          ConnectFailReason: DNS lookup failed
    //        --connect2
    //      --socket2Connect
    //      Failed.
    //    --connectInne


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.