Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
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
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Tcl) 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 Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

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

set tlsRead [new_CkSocket]

#  We'll just use an HTTPS server for this example...
set bUseTls 1
set maxWaitMs 5000
set success [CkSocket_Connect $tlsRead "www.chilkatsoft.com" 443 $bUseTls $maxWaitMs]
if {[expr $success != 1]} then {
    puts [CkSocket_lastErrorText $tlsRead]
    delete_CkSocket $tlsRead
    exit
}

#  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.

# tlsWrite is a CkSocket
set tlsWrite [CkSocket_CloneSocket $tlsRead]

#  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.
# task is a CkTask
set task [CkSocket_ReceiveUntilMatchAsync $tlsRead "\r\n\r\n"]
CkTask_Run $task

#  Now send the request.  This should not block because the read is happening on the tlsRead object.
set httpGetReq "GET / HTTP/1.1\r\nHost: www.chilkatsoft.com\r\n\r\n"
set success [CkSocket_SendString $tlsWrite $httpGetReq]
#  Assuming success for the example...

#  Wait for the read task to finish.
#  The 1/0 returned by Wait applies to the Wait method call, not the task.
set maxWaitMs 5000
set success [CkTask_Wait $task $maxWaitMs]
if {[expr !$success OR [expr [expr [CkTask_get_StatusInt $task] != 7] OR [expr [CkTask_get_TaskSuccess $task] != 1]]]} then {
    if {!$success} then {
        #  The task.LastErrorText applies to the Wait method call.
        puts [CkTask_lastErrorText $task]
    }     else {
        #  The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        puts [CkTask_status $task]
        puts [CkTask_resultErrorText $task]
    }

    delete_CkTask $task

    delete_CkSocket $tlsWrite

    delete_CkSocket $tlsRead
    exit
}

#  Examine the received HTTP response header:
puts "HTTP response header:"
puts [CkTask_getResultString $task]

#  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

delete_CkTask $task

delete_CkSocket $tlsWrite

#  Forget about the remainder of the HTTP response... The example was only to demonstrate
#  simultaneous reading/writing..
set maxWaitMs 20
CkSocket_Close $tlsRead $maxWaitMs

delete_CkSocket $tlsRead

 

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