2012-02-23 4 views
8

FTP를 사용하여 파일을 다운로드하려고하는데 연결이 끝나면 파일을 중단 한 위치에서 다시 시작해야합니다. 내 문제는 연결을 닫고 다시 연결하면 다음 코드 조각을 사용하여 다운로드를 계속할 수 있지만 서버 사이트에서 다운로드를 다시 시작할 수 없으면 프로그램이 무한 상태가됩니다.C에서 Curl을 사용하여 FTP를 통해 파일 다운로드를 재개하는 방법은 무엇입니까?

#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]/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; 
} 

원격 사이트가 연결을 끊으면 어떻게 다운로드를 재개 할 수 있습니까? 어떤 도움 appeand과의 끝에서 다운로드를 재개 할 libcurl에 말할 필요성을 인식하여 쓰기 기능에 FTPFILE 구조체에 가변적이고 추가 Yuvi

+0

ftp에 http의 'AddRange'와 같은 것이 있습니까? –

+0

네,하지만 거기에 내 문제는 연결을 닫으면 오류를 잡을 수 없다는 것입니다. 더 많은 정보를 확인하려면 여기를 클릭하십시오 http://curl.haxx.se/libcurl/c/ftpuploadresume.html – Yuvi

답변

2

감사합니다,

을 감상 할 수있다 바이트 수에 CURLOPT_RESUME_FROM을 설정하여 대상 파일이 이미 다운로드 :

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

를 주에서 다시 시작하려는 경우 :

,
curl_easy_setopt(curl, CURLOPT_RESUME_FROM , numberOfBytesToSkip); 

당신이 파일이 이미 존재하지 않거나은 다시 다운로드하지만, 새로운 다운로드가 아닌 경우, my_fwrite에서 0

다시 CURLOPT_RESUME_FROM을 설정해야합니다 :

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

추신 당신이 파일 (2GB) 긴보다 큰 다시 시작해야하는 위치를 알고하는 방법에 대한 자세한 정보를 요청하는 의견을 대응 CURLOPT_RESUME_FROM_LARGE 및 CURL_OFF_T_C()

로 보면 전송이 실패했을 때 :

을 한 후 컬 쉽게 수행 전화를 호출

CURLINFO_HEADER_SIZE 
CURLINFO_CONTENT_LENGTH_DOWNLOAD 

이 함께 추가하고 전자 확인 :

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

는 컬 컨텍스트에서 검색

qual

CURLINFO_SIZE_DOWNLOAD 

컨텍스트를 다시 작성하지 않으려는 경우.

최신 버전의 컬 (curl)을 사용하십시오. 다운로드하는 FTP 서버로부터 청각을 느끼지 못하면 60 초 후에 시간이 초과되어야합니다.

+0

thanks 대답을 위해,하지만 어떻게 연결이 원격 사이트에 의해 닫혀 있는지, 그 샘플 프로그램은 조건이 없다면, 그것은 무한 상태로 남아 있다는 것을 알 수 있습니까? – Yuvi

0

CURLOPT_CONNECTTIMEOUT 및 CURLOPT_TIMEOUT 매개 변수를 사용하여 핸들 당 연결 시간 초과 및 최대 실행 시간을 지정할 수 있습니다.

다른 방법 (멀티 인터페이스가 아닌 쉬운 인터페이스를 사용하는 경우에만 작동합니다)은 CURLOPT_SOCKOPTFUNCTION을 사용하여 설정할 수있는 소켓 옵션 콜백을 사용하는 것입니다. 이 경우 SO_RCVTIMEO 매개 변수에 대해 setsockopt()를 호출해야 연결이 끊어지기 전에 유휴 상태가 될 수있는 최대 시간이 필요합니다. 즉, 마지막 5 초 동안 바이트가 수신되지 않았다면 연결을 끊습니다.

관련 문제