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

PowerBuilder 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

 

 

 

(PowerBuilder) Transfer a File using Sockets (TLS or non-TLS)

Demonstrates how to two programs, one a socket writer and the other a socket reader, can transfer a file. The connection can be TLS or a regular non-encrypted TCP connection.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

integer li_rc
oleobject loo_BdToSend
integer li_Success
oleobject loo_SndSock
integer li_BUseTls
integer li_Port
integer li_MaxWaitMs
integer li_NumBytes
integer li_BBigEndian
oleobject loo_ListenSock
oleobject loo_RcvSock
integer li_NumBytesComing
oleobject loo_BdReceived
integer li_MaxWaitMs

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

// On the sending side, we'll load the file into a BinData object and send.
// On the receiving side, we'll read from the socket connection into a BinData, and save to a file.
// This example assumes the file is not crazy-large, and that the entire contents
// can fit into memory.  
// (If the file is too large for memory, there are other ways to send. It just involves streaming or 
// sending the file chunk-by-chunk..)

// This section of code is for the sender.
loo_BdToSend = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdToSend.ConnectToNewObject("Chilkat.BinData")
if li_rc < 0 then
    destroy loo_BdToSend
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_BdToSend.LoadFile("somePath/someFile.dat")
// Assume success for the example...

loo_SndSock = create oleobject
// Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0
li_rc = loo_SndSock.ConnectToNewObject("Chilkat.Socket")

li_BUseTls = 1
li_Port = 5555
li_MaxWaitMs = 5000
li_Success = loo_SndSock.Connect("some_domain_or_ip.com",li_Port,li_BUseTls,li_MaxWaitMs)
// Assume success for the example...

// Tell the receiver how many bytes are coming.
li_NumBytes = loo_BdToSend.NumBytes
li_BBigEndian = 1
li_Success = loo_SndSock.SendInt32(li_NumBytes,li_BBigEndian)

// Send the file data (sends the entire contents of bdToSend).
li_Success = loo_SndSock.SendBd(loo_BdToSend,0,0)

// Get an acknowledgement.
li_Success = loo_SndSock.ReceiveInt32(li_BBigEndian)
if li_Success <> 1 then
    Write-Debug loo_SndSock.LastErrorText
    destroy loo_BdToSend
    destroy loo_SndSock
    return
end if

// Did the receiver get the correct number of bytes?
if loo_SndSock.ReceivedInt <> li_NumBytes then
    Write-Debug "The receiver did not acknowledge with the correct number of bytes."
    destroy loo_BdToSend
    destroy loo_SndSock
    return
end if

Write-Debug "File sent!"

// ------------------------------------------------------------------------------------
// The code below is for the receiving side (running on some other computer..)

loo_ListenSock = create oleobject
// Use "Chilkat_9_5_0.Socket" for versions of Chilkat < 10.0.0
li_rc = loo_ListenSock.ConnectToNewObject("Chilkat.Socket")

li_Success = loo_ListenSock.BindAndListen(5555,25)
if li_Success <> 1 then
    Write-Debug loo_ListenSock.LastErrorText
    destroy loo_BdToSend
    destroy loo_SndSock
    destroy loo_ListenSock
    return
end if

// Get the next incoming connection
// Wait a maximum of 20 seconds (20000 millisec)

loo_RcvSock = loo_ListenSock.AcceptNextConnection(20000)
if loo_ListenSock.LastMethodSuccess = 0 then
    Write-Debug loo_ListenSock.LastErrorText
    destroy loo_BdToSend
    destroy loo_SndSock
    destroy loo_ListenSock
    return
end if

// The sender will first send the big-endian integer for the number of bytes
// that are forthcoming..
li_Success = loo_RcvSock.ReceiveInt32(li_BBigEndian)
if li_Success <> 1 then
    Write-Debug loo_RcvSock.LastErrorText
    destroy loo_RcvSock
    destroy loo_BdToSend
    destroy loo_SndSock
    destroy loo_ListenSock
    return
end if

li_NumBytesComing = loo_RcvSock.ReceivedInt

// Receive that many bytes..
loo_BdReceived = create oleobject
// Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
li_rc = loo_BdReceived.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_RcvSock.ReceiveBdN(li_NumBytesComing,loo_BdReceived)
if li_Success <> 1 then
    Write-Debug loo_RcvSock.LastErrorText
    destroy loo_RcvSock
    destroy loo_BdToSend
    destroy loo_SndSock
    destroy loo_ListenSock
    destroy loo_BdReceived
    return
end if

// Acknowledge the sender by sending back the number of bytes we received.
li_Success = loo_RcvSock.SendInt32(loo_BdReceived.NumBytes,li_BBigEndian)

// Close the connection.
li_MaxWaitMs = 20
loo_RcvSock.Close(li_MaxWaitMs)
destroy loo_RcvSock

// Save the received data to a file.
li_Success = loo_BdReceived.WriteFile("somePath/someFile.dat")
// Assume success for the example...

Write-Debug "File received!"


destroy loo_BdToSend
destroy loo_SndSock
destroy loo_ListenSock
destroy loo_BdReceived

 

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