2012-02-23 43 views
8

我试图使用FTP下载文件,如果连接终止,那么它应该从停止的地方恢复。我的问题是,如果我关闭连接然后重新连接它,使用下面的代码段能够继续下载,但如果我在服务器站点这样做,那么我无法恢复下载,并且程序进入无限状态。如何通过C使用Curl恢复文件下载?

#include <stdio.h> 

#include <curl/curl.h> 

/* 
* This is an example showing how to get a single file from an FTP server. 
* It delays the actual destination file creation until the first write 
* callback so that it won't create an empty file in case the remote file 
* doesn't exist or something else fails. 
*/ 

struct FtpFile { 
    const char *filename; 
    FILE *stream; 
}; 

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) 
{ 
    struct FtpFile *out=(struct FtpFile *)stream; 
    if(out && !out->stream) { 
    /* open file for writing */ 
    out->stream=fopen(out->filename, "wb"); 
    if(!out->stream) 
     return -1; /* failure, can't open file to write */ 
    } 
    return fwrite(buffer, size, nmemb, out->stream); 
} 


int main(void) 
{ 
    CURL *curl; 
    CURLcode res; 
    struct FtpFile ftpfile={ 
    "dev.zip", /* name to store the file as if succesful */ 
    NULL 
    }; 

    curl_global_init(CURL_GLOBAL_DEFAULT); 

    curl = curl_easy_init(); 
    if(curl) { 
    /* 
    * You better replace the URL with one that works! 
    */ 
    curl_easy_setopt(curl, CURLOPT_URL, 
        "ftp://root:[email protected].1/dev.zip"); 
    /* Define our callback to get called when there's data to be written */ 
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite); 
    /* Set a pointer to our struct to pass to the callback */ 
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile); 

    /* Switch on full protocol/debug output */ 
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 

    res = curl_easy_perform(curl); 

    /* always cleanup */ 
    curl_easy_cleanup(curl); 

    if(CURLE_OK != res) { 
     /* we failed */ 
     fprintf(stderr, "curl told us %d\n", res); 
    } 
    } 

    if(ftpfile.stream) 
    fclose(ftpfile.stream); /* close the local file */ 

    curl_global_cleanup(); 

    return 0; 
} 

任何人都可以告诉我,如果远程站点关闭连接,我该如何恢复下载。 任何帮助,将不胜感激

感谢,

Yuvi

+0

有没有像HTTP的'AddRange'在ftp? – 2012-02-23 10:39:33

+0

是的,它是存在的,但我的问题是,如果连接已关闭,我无法捕获错误,要知道更多,你可以在这里检查它http://curl.haxx.se/libcurl/c/ftpuploadresume.html谢谢 – Yuvi 2012-02-23 11:03:44

回答

2

一个varaible添加到ftpfile结构到必须意识到你写功能appeand告诉libcurl的从底了恢复下载通过设置CURLOPT_RESUME_FROM到的字节数目标文件已经下载:

struct FtpFile 
{ 
    const char *pcInfFil; 
    FILE *pFd; 
    int iAppend; 
}; 

在主,如果你想恢复:

curl_easy_setopt(curl, CURLOPT_RESUME_FROM , numberOfBytesToSkip); 

如果该文件不存在,或者它不是一个恢复的下载,但一个新的下载,一定要设置CURLOPT_RESUME_FROM回到0

在my_fwrite:

out->stream=fopen(out->filename, out->iAppend ? "ab":"wb"); 

PS如果你需要恢复的文件比长(2GB)大,考虑CURLOPT_RESUME_FROM_LARGE和CURL_OFF_T_C()

在回应置评请求如何知道更多的信息,当传输失败:

你之后调用卷曲容易执行呼叫:

CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...); 

来自卷边上下文检索:

CURLINFO_HEADER_SIZE 
CURLINFO_CONTENT_LENGTH_DOWNLOAD 

一起加入他们,并确保他们Ë qual

CURLINFO_SIZE_DOWNLOAD 

如果不尝试重新执行上下文。

并且一定要使用最新版本的curl,它应该在60秒内没有从正在下载的FTP服务器收到信息时超时。

+0

谢谢为了回答,但我如何才能知道该连接是否被远程站点关闭,如果条件不变,示例程序不会运行,它仍处于无限状态。 – Yuvi 2012-02-24 05:35:18

0

您可以使用CURLOPT_CONNECTTIMEOUT和CURLOPT_TIMEOUT参数来指定每个句柄的连接超时和最大执行时间。

另一种方式(只适用于使用简单界面而不适用于多功能界面)的方法是使用套接字选项回调,您可以使用CURLOPT_SOCKOPTFUNCTION进行设置。其中,必须调用SO_RCVTIMEO参数的setsockopt(),以使连接在被删除之前可以空闲的最长时间。即如果在最后5秒内没有收到字节,则丢弃连接。