Sample code for 30+ languages & platforms
C++

Check FTP Connection and Login Status

See more FTP Examples

Demonstrates the connection-status properties ConnectVerified, LoginVerified, ConnectFailReason, and Greeting.

Background: When Connect fails, these properties separate the possible causes: ConnectVerified shows whether the TCP connection was established at all, LoginVerified whether authentication then succeeded, and ConnectFailReason gives a numeric code pinpointing the failure — far more actionable than a generic error for distinguishing, say, an unreachable host from a bad password. The Greeting captures the server's opening banner, sometimes useful for identifying the server or its policies.

Chilkat C++ Downloads

C++
#include <CkFtp2.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkFtp2 ftp;

    ftp.put_Hostname("ftp.example.com");
    ftp.put_Username("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.
    ftp.put_Password("myPassword");

    success = ftp.Connect();
    if (success != true) {
        //  ConnectFailReason gives a numeric code describing why Connect failed.
        std::cout << "Connect failed, reason code: " << ftp.get_ConnectFailReason() << "\r\n";
        std::cout << ftp.lastErrorText() << "\r\n";
        return;
    }

    //  After a successful connect, these flags report what succeeded.
    std::cout << "TCP connection verified: " << ftp.get_ConnectVerified() << "\r\n";
    std::cout << "Login verified: " << ftp.get_LoginVerified() << "\r\n";

    //  The Greeting property holds the server's initial greeting banner.
    std::cout << "Server greeting: " << ftp.greeting() << "\r\n";

    success = ftp.Disconnect();
    if (success == false) {
        std::cout << ftp.lastErrorText() << "\r\n";
        return;
    }
    }