Sample code for 30+ languages & platforms
PureBasic

Connect to an FTP Server Without Logging In

See more FTP Examples

Demonstrates the Chilkat Ftp2.ConnectOnly method, which establishes the connection (including any proxy and TLS setup) and receives the greeting, but does not send USER or PASS. It takes no arguments; authentication is completed with LoginAfterConnectOnly.

Background: Splitting connect from login gives you a window between the two — to read the greeting, decide which account to use, or set up TLS options that depend on the server — that the all-in-one Connect does not. After ConnectOnly the control connection is open and CheckConnection returns true even though the session is not yet authenticated.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.ConnectOnly method, which establishes the connection (including any proxy
    ;  and TLS setup) and receives the greeting, but does NOT send USER or PASS.  It takes no
    ;  arguments.  Authentication is completed separately with LoginAfterConnectOnly.

    ftp.i = CkFtp2::ckCreate()
    If ftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkPort(ftp, 21)

    ;  Connect without logging in.
    success = CkFtp2::ckConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    Debug "Connected (not yet logged in)."

    ;  The greeting is now available; credentials can be set based on it, then authenticate.
    CkFtp2::setCkUsername(ftp, "myFtpLogin")
    CkFtp2::setCkPassword(ftp, "myPassword")

    success = CkFtp2::ckLoginAfterConnectOnly(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    Debug "Logged in."

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf



    CkFtp2::ckDispose(ftp)


    ProcedureReturn
EndProcedure