使用libcurl来监控网络状态

时间:2011-02-27 15:16:01

标签: c libcurl

我需要一个每秒轮询一个站点的程序,如果该站点在15秒内没有响应则会响应。我从示例程序中做了以下hack。在干运行中,它在15秒内打印7次。我不能让curl_easy_perform在响应前等待15秒吗?

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 个答案:

答案 0 :(得分:2)

Sleep(15000 - timeTakenForCurlInMs);

答案 1 :(得分: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;
}