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
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) Bidirectional Sockets (TLS or non-TLS, simultaneous reading and writing a connection)

This example demonstrates how to simultaneously read/write on a single socket connection.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-9.5.0-win32.pkg

Procedure Test
    Handle hoTlsRead
    Boolean iBUseTls
    Integer iMaxWaitMs
    Boolean iSuccess
    Variant vTlsWrite
    Handle hoTlsWrite
    Variant vTask
    Handle hoTask
    String sHttpGetReq
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

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

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

    // We'll just use an HTTPS server for this example...
    Move True To iBUseTls
    Move 5000 To iMaxWaitMs
    Get ComConnect Of hoTlsRead "www.chilkatsoft.com" 443 iBUseTls iMaxWaitMs To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoTlsRead To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Chilkat classes are thread-safe.  This means that only one method call can be active
    // at a time for a given object instance.  It would seem that this would prevent the possibility
    // to simultaneously read/write a given connection because it would require two method calls
    // to be simultaneously active: one for reading and one for writing.
    // 
    // There's a trick to doing it...
    // 
    // The CloneSocket method is provided to get a new object instance that shares the same socket
    // connection.  This allows for the coarse-grained object-level thread safety to be maintained, 
    // while finer-grained thread-safety mechanisms keep things kosher internally.

    // One object will be used for reading, and the cloned socket is used for writing.
    // It doesn't matter which --  you can use the cloned socket for reading or the original for writing.
    // However.. if you try to read simultneously from both the original and cloned objects at the same
    // time, then one will block until the other finishes.  (This is because of the finer-grained thread
    // safety internally.)  The same is true if you try to write both socket objects simultaneously.

    Get ComCloneSocket Of hoTlsRead To vTlsWrite
    If (IsComObject(vTlsWrite)) Begin
        Get Create (RefClass(cComChilkatSocket)) To hoTlsWrite
        Set pvComObject Of hoTlsWrite To vTlsWrite
    End

    // Let's start an async read on the socket.  Nothing will be arriving until we actually send the GET
    // request and the server responds.  This will read until the end of the HTTP response header.
    Get ComReceiveUntilMatchAsync Of hoTlsRead (character(13)) + (character(10)) + (character(13)) + (character(10)) To vTask
    If (IsComObject(vTask)) Begin
        Get Create (RefClass(cComChilkatTask)) To hoTask
        Set pvComObject Of hoTask To vTask
    End
    Get ComRun Of hoTask To iSuccess

    // Now send the request.  This should not block because the read is happening on the tlsRead object.
    Move "GET / HTTP/1.1" + (character(13)) + (character(10)) + "Host: www.chilkatsoft.com" + (character(13)) + (character(10)) + (character(13)) + (character(10)) To sHttpGetReq
    Get ComSendString Of hoTlsWrite sHttpGetReq To iSuccess
    // Assuming success for the example...

    // Wait for the read task to finish.
    // The True/False returned by Wait applies to the Wait method call, not the task.
    Move 5000 To iMaxWaitMs
    Get ComWait Of hoTask iMaxWaitMs To iSuccess
    Get ComStatusInt Of hoTask To iTemp1
    Get ComTaskSuccess Of hoTask To bTemp1
    If (Not iSuccess Or (iTemp1 <> 7) Or (bTemp1 <> True)) Begin
        If (Not iSuccess) Begin
            // The task.LastErrorText applies to the Wait method call.
            Get ComLastErrorText Of hoTask To sTemp1
            Showln sTemp1
        End
        Else Begin
            // The ResultErrorText applies to the underlying task method call (i.e. the Connect)
            Get ComStatus Of hoTask To sTemp1
            Showln sTemp1
            Get ComResultErrorText Of hoTask To sTemp1
            Showln sTemp1
        End

        Send Destroy of hoTask
        Send Destroy of hoTlsWrite
        Procedure_Return
    End

    // Examine the received HTTP response header:
    Showln "HTTP response header:"
    Get ComGetResultString Of hoTask To sTemp1
    Showln sTemp1

    // We should get a response that looks like this:
    // 	HTTP response header:
    // 	HTTP/1.1 200 OK
    // 	Cache-Control: private
    // 	Content-Length: 7477
    // 	Content-Type: text/html
    // 	Server: Microsoft-IIS/8.5
    // 	Set-Cookie: ASPSESSIONIDSWDSTRTQ=BBNMIKGCHFJNILFFPLDIOGDE; secure; path=/
    // 	X-Powered-By: ASP.NET
    // 	X-Powered-By-Plesk: PleskWin
    // 	Date: Thu, 06 Apr 2017 12:03:30 GMT

    Send Destroy of hoTask
    Send Destroy of hoTlsWrite

    // Forget about the remainder of the HTTP response... The example was only to demonstrate
    // simultaneous reading/writing..
    Move 20 To iMaxWaitMs
    Get ComClose Of hoTlsRead iMaxWaitMs To iSuccess


End_Procedure

 

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