libcurl — Keep Connection “open” to Upload Multipl

2019-02-16 16:16发布

问题:

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);

回答1:

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.



标签: c curl ftp libcurl