Sample code for 30+ languages & platforms
Tcl

Monitor, Cancel, and Keep-Alive Long FTP Transfers

See more FTP Examples

Demonstrates the HeartbeatMs, AbortCurrent, and LargeFileMeasures properties, which monitor long operations, allow cancellation, and keep the control connection alive during long transfers.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: A large transfer blocks for a long time, and these properties make that manageable. HeartbeatMs fires a periodic AbortCheck callback so an application can show progress and remain responsive, and AbortCurrent — set from that callback or another thread — cancels the running operation. LargeFileMeasures addresses a subtler problem: while data flows on the data connection, an idle control connection can be dropped by a firewall, so this sends a periodic NOOP to keep it alive.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set ftp [new_CkFtp2]

CkFtp2_put_Hostname $ftp "ftp.example.com"
CkFtp2_put_Username $ftp "myFtpLogin"

#  Normally you would not hard-code the password in source.  You should instead obtain it
#  from an interactive prompt, environment variable, or a secrets vault.
CkFtp2_put_Password $ftp "myPassword"

#  HeartbeatMs sets how often the AbortCheck event fires during long operations.  The default 0
#  disables it.  A nonzero value lets an application monitor progress and request cancellation.
CkFtp2_put_HeartbeatMs $ftp 100

#  AbortCurrent may be set to 1 (typically from another thread, or from an AbortCheck
#  callback) to cancel a running operation.  Left 0 here so nothing is pre-emptively aborted.
CkFtp2_put_AbortCurrent $ftp 0

#  LargeFileMeasures (default 0) sends a NOOP on the control channel once per minute during
#  a long transfer, keeping the control connection alive while data flows.
CkFtp2_put_LargeFileMeasures $ftp 1

set success [CkFtp2_Connect $ftp]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    exit
}

set success [CkFtp2_GetFile $ftp "public_html/hugefile.iso" "qa_output/hugefile.iso"]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    exit
}

puts "Transfer complete."

set success [CkFtp2_Disconnect $ftp]
if {$success == 0} then {
    puts [CkFtp2_lastErrorText $ftp]
    delete_CkFtp2 $ftp
    exit
}


delete_CkFtp2 $ftp