Sample code for 30+ languages & platforms
C

Capture the FTP Session Log

See more FTP Examples

Demonstrates the SessionLog property (with KeepSessionLog), which holds the in-memory FTP protocol conversation — commands and server replies — collected during the session.

Background: When an FTP operation fails for a non-obvious reason, the exact command/reply exchange is the most useful diagnostic there is, and SessionLog captures it verbatim once KeepSessionLog is enabled. Passwords are omitted, so it is safe to log or attach to a bug report. Over a long session it grows without bound; clear it with ClearSessionLog to bound memory or to isolate the log for one operation.

Chilkat C Downloads

C
#include <C_CkFtp2.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2 ftp;

    success = FALSE;

    ftp = CkFtp2_Create();

    CkFtp2_putHostname(ftp,"ftp.example.com");
    CkFtp2_putUsername(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_putPassword(ftp,"myPassword");

    //  Keep an in-memory log of the FTP protocol conversation for troubleshooting.  Passwords are
    //  not recorded in it.
    CkFtp2_putKeepSessionLog(ftp,TRUE);

    success = CkFtp2_Connect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    success = CkFtp2_ChangeRemoteDir(ftp,"public_html");
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }

    //  The SessionLog contains the protocol commands and server replies collected so far.
    printf("%s\n",CkFtp2_sessionLog(ftp));

    success = CkFtp2_Disconnect(ftp);
    if (success == FALSE) {
        printf("%s\n",CkFtp2_lastErrorText(ftp));
        CkFtp2_Dispose(ftp);
        return;
    }



    CkFtp2_Dispose(ftp);

    }