libcurl — 保持连接“打开";上传多个文件 (FTP) [英] libcurl — Keep Connection "open" to Upload Multiple Files (FTP)

查看:33
本文介绍了libcurl — 保持连接“打开";上传多个文件 (FTP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将目录上传到我的应用程序上的 FTP 服务器,并计划使用 libcurl.我看到没有直接的方法来上传一个有很多的目录文件,这对我来说很有意义.但是,我找不到任何提及上传许多文件.

I need to upload directories to a FTP server on my application, and plan to use libcurl. I see there is no direct way to upload a directory with many files, which makes sense to me. I couldn't, however, find any mention on uploading many files.

如果我得到目录中的文件列表,我可以循环上传它们.选项 CURLOPT_FTP_CREATE_MISSING_DIRS 可能有助于子目录,但如果我也想知道我是否错过了这里的重点,或者这会有任何严重的缺点.

If I get the list of files in the directory, I could upload them in a loop. The option CURLOPT_FTP_CREATE_MISSING_DIRS might help with sub-directories, but if I'd like to know also if I'm missing the point here or this would have any serious drawback.

主要问题是:如何保持连接打开"?重新连接每个文件都可能意味着额外的不必要的开销.

The main question is: how can I keep the connection "open"? Reconnecting on each file would probably mean an extra unwanted overhead.

理想情况下,我想继续使用简单的界面.但如果另一个接口在这种情况下提供了更好的支持,我会使用它.

Ideally, I'd like to keep using the easy interface. But if another interface provides better support in this case, I'll use it.

CURLcode ret;
CURL *handle = curl_easy_init();

/* Connect to FTP server using     *
 * the given username and password */

for ({each file}) {

    curl_easy_setopt(handle, ..., ...);
    ...
    ret = curl_easy_perform(handle);
    /* Analyse return code */
    curl_easy_reset(handle);
}

/* Disconnect from server */
curl_easy_clenup(handle);

推荐答案

只要重复使用同一个句柄,它会尽可能保持连接打开,后续传输会重复使用前一个.

Just re-use the same handle, and it will keep the connection open as much as possible and subsequent transfers will re-use the previous one.

当您使用简易接口时,连接缓存保存在简易句柄中.如果您改为使用多接口,则连接缓存将保存在多句柄中,并在同一多句柄中使用的所有简单句柄之间共享.

When you use the easy interface, the connection cache is kept within the easy handle. If you instead use the multi interface, the connection cache will be kept within the multi handle and will be shared among all the easy handles that are used within the same multi handle.

这篇关于libcurl — 保持连接“打开";上传多个文件 (FTP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆