2011-02-27 48 views
1

我需要一个程序,每秒轮询一个站点并响应,如果站点在15秒内没有响应。我从一个示例程序中进行了以下攻击。在空运行中,它在15秒内打印7次。我不能让curl_easy_perform在响应之前等待15秒吗?使用libcurl监视网络的状态

int main(void) 
{ 
    CURL *curl; 
CURLcode res; 
char *postthis="moo mooo moo moo"; 

curl = curl_easy_init(); 
curl_easy_setopt(curl, CURLOPT_URL, "http://192.168.1.101"); 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postthis); 
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(postthis)); 
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 15); 

while(1) 
{ 
    Sleep(1000); 
    res = curl_easy_perform(curl); 
    if(res!= CURLE_OK) 
     printf("nada \n"); 

} 

回答

2
Sleep(15000 - timeTakenForCurlInMs); 
+0

将试图实现,这是内置的功能。钉住睡眠()和转移时间,然后总是有一个小的处理时间。这不会太重要。 – aitchnyu 2011-02-27 18:23:37

2

这是一个愚蠢的黑客工具,失败每一秒。它基于POST教程。我砍了GET的东西,它工作正常。

curl = curl_easy_init(); 
    if(curl) { 
    curl_easy_setopt(curl, CURLOPT_URL, "http://URL_HERE"); 
    **curl_easy_setopt(curl, CURLOPT_TIMEOUT, 5); 
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);** 

while(1) 
{ 
    Sleep(1000); 
    res = curl_easy_perform(curl); 
    if(res== CURLE_OK) printf("\n\n yeah \n"); 
    else printf("\n\n nada \n"); 

} 
    /* always cleanup */ 
    curl_easy_cleanup(curl); 
    } 
    return 0; 
}