2010-06-16 3 views
3

아래 코드는 CURL C API에 대한 테스트입니다. 문제는 콜백 함수 write_callback이 호출되지 않는다는 것입니다. 왜 ?CURL C API : 콜백이 호출되지 않았습니다.

/** compilation: g++ source.cpp -lcurl */ 

#include <assert.h> 
#include <iostream> 
#include <cstdlib> 
#include <cstring> 
#include <cassert> 
#include <curl/curl.h> 

using namespace std; 

static size_t write_callback(void *ptr, size_t size, size_t nmemb, void *userp) 
{ 
    std::cerr << "CALLBACK WAS CALLED" << endl; 
    exit(-1); 
    return size*nmemb; 
} 

static void test_curl() 
{ 
    int any_data=1; 
    CURLM* multi_handle=NULL; 
    CURL* handle_curl = ::curl_easy_init(); 
    assert(handle_curl!=NULL); 
    ::curl_easy_setopt(handle_curl, CURLOPT_URL, "http://en.wikipedia.org/wiki/Main_Page"); 
    ::curl_easy_setopt(handle_curl, CURLOPT_WRITEDATA, &any_data); 
    ::curl_easy_setopt(handle_curl, CURLOPT_VERBOSE, 1); 
    ::curl_easy_setopt(handle_curl, CURLOPT_WRITEFUNCTION, write_callback); 
    ::curl_easy_setopt(handle_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); 

    multi_handle = ::curl_multi_init(); 
    assert(multi_handle!=NULL); 
    ::curl_multi_add_handle(multi_handle, handle_curl); 
    int still_running=0; 
    /* lets start the fetch */ 
    while(::curl_multi_perform(multi_handle, &still_running) == 
      CURLM_CALL_MULTI_PERFORM); 
    std::cerr << "End of curl_multi_perform."<< endl; 
    //cleanup should go here 
    ::exit(EXIT_SUCCESS); 
} 

int main(int argc,char** argv) 
{ 
    test_curl(); 
    return 0; 
} 

많은 감사

피에르

답변

6

당신은 still_running의 값을 확인하고 여전히 작업이 보류중인 경우 다시 curl_multi_perform()를 호출해야합니다.

간단한 예 :

int still_running=0; 
/* lets start the fetch */ 
do { 
    while(::curl_multi_perform(multi_handle, &still_running) == 
     CURLM_CALL_MULTI_PERFORM); 
} while (still_running); 
+2

하지만 당신은 선택 과정() 또는 (당신은 그냥이 예제를 복사 할 경우), 그렇지 않으면 같은뿐만 아니라, 그 사이에 유사한의 프로그램이 미친 듯이 바쁜 루프와 100 %의 CPU를 사용합니다 –

+0

@DanielStenberg 당신은 무엇을 의미합니까? 이 예제를 test_curl 함수에 추가 할 수 없습니까? 뭐가 잘못 됐어? –

+0

당신의 프로그램은 미친 듯이 바쁜 루프를 일으킬 것입니다. 결코 멈추지 않거나 일시 중지되거나 기다려야 할 때입니다. –

관련 문제