Sample code for 30+ languages & platforms
PureBasic

Disconnect from an FTP Server

See more FTP Examples

Demonstrates the Chilkat Ftp2.Disconnect method, which ends the FTP session and closes the control connection. It takes no arguments. When connected, Chilkat sends QUIT and then closes the socket.

Background: Sending QUIT before closing is the polite shutdown — it lets the server finalize the session cleanly rather than seeing an abrupt socket drop. The method is idempotent, so calling it when nothing is connected, or a second time after the session is already closed, still succeeds; that makes it safe to put in cleanup paths without guarding it.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.Disconnect method, which ends the FTP session and closes the control
    ;  connection.  It takes no arguments.  When connected, Chilkat sends QUIT and then closes the
    ;  socket.

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

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkUsername(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::setCkPassword(ftp, "myPassword")

    ;  Connect and log in (Connect performs both).
    success = CkFtp2::ckConnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ;  ... perform FTP operations ...

    ;  Disconnect is idempotent: calling it when no connection exists, or calling it again after the
    ;  session is already closed, still succeeds.
    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    Debug "Disconnected."


    CkFtp2::ckDispose(ftp)


    ProcedureReturn
EndProcedure