Unicode C
Unicode C
Close a Remote SFTP File or Directory Handle
See more SFTP Examples
Demonstrates the Chilkat SFtp.CloseHandle method, which closes a remote file or directory handle previously returned by OpenFile or OpenDir. The only argument is the handle.
Background: Every opened handle corresponds to state the server holds, and servers cap how many a session may keep open at once, so a long-running program that forgets to close handles will eventually start failing to open new ones. Closing also ensures buffered writes are committed. Once closed, a handle is invalid and must not be reused.
Chilkat Unicode C Downloads
#include <C_CkSFtpW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSFtpW sftp;
int port;
const wchar_t *password;
const wchar_t *handle;
success = FALSE;
// Demonstrates the SFtp.CloseHandle method, which closes a remote file or directory handle. The
// only argument is the handle returned by OpenFile or OpenDir.
sftp = CkSFtpW_Create();
// Connect, authenticate, and initialize the SFTP subsystem.
port = 22;
success = CkSFtpW_Connect(sftp,L"sftp.example.com",port);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// 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.
password = L"mySshPassword";
success = CkSFtpW_AuthenticatePw(sftp,L"mySshLogin",password);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
success = CkSFtpW_InitializeSftp(sftp);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// Open a remote file, obtaining a handle.
handle = CkSFtpW_openFile(sftp,L"subdir/data.txt",L"readOnly",L"openExisting");
if (CkSFtpW_getLastMethodSuccess(sftp) == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
// ... use the handle ...
// Close the handle to release the server-side resource. A handle should not be used after it
// is closed.
success = CkSFtpW_CloseHandle(sftp,handle);
if (success == FALSE) {
wprintf(L"%s\n",CkSFtpW_lastErrorText(sftp));
CkSFtpW_Dispose(sftp);
return;
}
wprintf(L"Handle closed.\n");
CkSFtpW_Disconnect(sftp);
CkSFtpW_Dispose(sftp);
}