libcurl的 - 保持连接"开放"要上传多个文件(FTP) [英] libcurl — Keep Connection "open" to Upload Multiple Files (FTP)

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

问题描述

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

如果我得到目录中的文件列表中,我可以上传他们在一个循环中。
选项​​ CURLOPT_FTP_CREATE_MISSING_DIRS 可能与子目录帮助,
但如果我想也知道如果我错过了点,这里还是这个本来
任何严重的缺点。

主要的问题是:我怎么能保持连接开放?重新连接上
每个文件可能意味着一个额外的不必要的开销。

在理想情况下,我想用简单的界面保持。但是,如果其他接口
在这种情况下提供更好的支持,我会使用它。

 卷曲code RET;
卷曲*手柄= curl_easy_init();/ *使用连接到FTP服务器*
 *给定的用户名和密码* /对于({每个文件}){    curl_easy_setopt(手柄,...,...);
    ...
    RET = curl_easy_perform(句柄);
    / *回报分析code * /
    curl_easy_reset(手柄);
}/ *从服务器断开连接* /
curl_easy_clenup(手柄);


解决方案

刚刚重新使用相同的手柄,它会保持连接打开尽可能多的和后续的转移将重新使用previous之一。

当您使用简单的接口,连接高速缓存保存在容易手柄内。如果改为使用多接口,连接高速缓存将被保留的多手柄内,并将所有被相同的多手柄内使用的容易手柄之间共享。

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.

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天全站免登陆