PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkFtp2.pb"
Procedure ChilkatExample()
success.i = 0
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")
success = CkFtp2::ckConnect(ftp)
If success <> 1
; ConnectFailReason gives a numeric code describing why Connect failed.
Debug "Connect failed, reason code: " + Str(CkFtp2::ckConnectFailReason(ftp))
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
; After a successful connect, these flags report what succeeded.
Debug "TCP connection verified: " + Str(CkFtp2::ckConnectVerified(ftp))
Debug "Login verified: " + Str(CkFtp2::ckLoginVerified(ftp))
; The Greeting property holds the server's initial greeting banner.
Debug "Server greeting: " + CkFtp2::ckGreeting(ftp)
success = CkFtp2::ckDisconnect(ftp)
If success = 0
Debug CkFtp2::ckLastErrorText(ftp)
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndIf
CkFtp2::ckDispose(ftp)
ProcedureReturn
EndProcedure