![](/img/trans.png)
[英]libssh2 SFTP pipelining multiple files: sftp_read() internal error
[英]perfomance libssh2 sftp upload multiple files
我嘗試通過libssh2 sftp子系統將多個文件上傳到遠程服務器。 我的問題是,在傳輸一個文件后,我必須始終關閉sftp句柄和sftp會話,否則函數libssh2_sftp_open()將失敗,並顯示“ FXP_STATUS 4,無法打開/創建文件” 。 由於會話和文件的這種重新初始化,我浪費了寶貴的時間。
有沒有一種方法可以循環上傳多個文件而無需重新初始化會話和/或句柄? 我希望此代碼段將向您展示我的問題,這里經過一個循環遍歷后重新初始化可以正常工作,但是需要4秒才能上傳4 KB:
// Transfer at most MAXTRANSFER files
for (i=0;(i < ((MAXTRANSFER<nfiles)?MAXTRANSFER:nfiles)) && (iRet == SSHH_OK); i++) {
localfile = malloc(sizeof(char) * (strlen(directory) + strlen(fnames[i]) + 2));
strcpy(localfile, directory);
strcat(localfile, "/");
strcat(localfile, fnames[i]);
local = fopen(localfile, "rb");
if (!local) {
dbgPrintFormat(DEBUG_WARNING, "dir_sftp: Can't open local file %s", localfile);
iRet = SSHH_ERR_SETTINGS;
}
// Init SFTP
if (iRet == SSHH_OK) {
sftp_session = libssh2_sftp_init(session);
if (!sftp_session) {
dbgPrintFormat(DEBUG_ERROR, "dir_sftp: Unable to init SFTP session (%d)", sftp_session);
iRet = SSHH_ERR_SETTINGS;
}
}
if (iRet == SSHH_OK) {
stat(localfile, &fileinfo);
char *dstfile = malloc(sizeof(char) * (strlen(destdir) + strlen(fnames[i]) + 2));
strcpy(dstfile, destdir);
strcat(dstfile, "/");
strcat(dstfile, fnames[i]);
dbgPrintFormat(DEBUG_FINE, "dir_sftp: Start sending file %s", localfile);
// Request a file via SFTP
sftp_handle = libssh2_sftp_open(sftp_session, dstfile,
LIBSSH2_FXF_WRITE|LIBSSH2_FXF_CREAT|LIBSSH2_FXF_TRUNC,
LIBSSH2_SFTP_S_IRWXU|LIBSSH2_SFTP_S_IRGRP|LIBSSH2_SFTP_S_IXGRP);
if (!sftp_handle) {
dbgPrintFormat(DEBUG_ERROR, "dir_sftp: Unable to open file with SFTP");
iRet = SSHH_ERR_SETTINGS;
}
if (iRet == SSHH_OK) {
dbgPrintFormat(DEBUG_FINE, "dir_sftp: SFTP session waiting to send file %s",localfile);
do {
nread = fread(mem, 1, sizeof(mem), local);
if (nread <= 0) {
/* end of file */
break;
}
ptr = mem;
do {
/* write the same data over and over, until EOF */
rc = libssh2_sftp_write(sftp_handle, ptr, nread);
// EOF
if(rc < 0)
break;
ptr += rc;
nread -= rc;
} while (nread);
} while (rc > 0);
}
ulSftpTxTryCount[iSftpTxTryCountIdx]++;
if (iRet == SSHH_OK) {
// Remove localfile *** if no error
if (remove(localfile) == -1) {
dbgPrintFormat(DEBUG_WARNING,"dir_sftp: Error removing file: %s", localfile);
}
if( successCnt ) {
(*successCnt)++;
}
dbgPrintFormat(DEBUG_FINE, "dir_sftp: File sent after %lu tries", ulSftpTxTryCount[iSftpTxTryCountIdx]);
iSftpTxTryCountIdx = (iSftpTxTryCountIdx + 1) % SCP_TRYCNT_NOF;
ulSftpTxTryCount[iSftpTxTryCountIdx] = 0;
}
free (dstfile);
}
if (local)
fclose(local);
free (localfile);
// Close file sftp handle
if (libssh2_sftp_close(sftp_handle) < 0) {
dbgPrintFormat(DEBUG_WARNING, "dir_sftp: Error closing SFTP handle ");
}
// Close sftp session
libssh2_sftp_shutdown(sftp_session);
}
你有幾個問題。 第一個問題(您要問的問題)是服務器似乎不希望客戶端同時打開多個文件。 即,這可能是服務器特定的問題(以及libssh2的問題)。
更大的問題是,您發送一個塊,等待結果然后發送另一個塊的方法非常慢。 當並行異步發送多個塊時,第三方SFTP庫實現了所謂的流水線。 這樣可以大大加快文件傳輸速度。
我的建議是找到一些專門為文件傳輸編寫的第三方庫。 這樣可以提高速度。
以下是嘗試最大化SFTP傳輸速度時要檢查的項目:
是否有防火牆或網絡設備正在檢查或限制網絡上的SSH2流量? 那可能會減慢速度。 檢查防火牆設置。 我們已經讓用戶報告通過修改防火牆規則來解決極其緩慢的SFTP文件傳輸。 您使用的SFTP客戶端可能會有很大的不同。 嘗試幾個不同的SFTP客戶端,看看是否得到不同的結果。 網絡延遲將嚴重影響SFTP。 如果您所使用的鏈接具有較高的延遲,那么快速傳輸將成為一個問題。 服務器計算機的功能強大嗎? SFTP的加密非常密集。 確保您擁有一台功能強大的計算機,該計算機在SFTP文件傳輸過程中不會造成負擔(CPU使用率高)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.