首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libcurl监视网络状态

使用libcurl监视网络状态
EN

Stack Overflow用户
提问于 2011-02-27 23:16:01
回答 2查看 358关注 0票数 1

我需要一个程序,轮询网站每秒和响应,如果网站没有在15秒内回应。我从一个示例程序中编写了以下代码。在试运行时,它在15秒内打印了7次。我不能让curl_easy_perform在响应前等待15秒吗?

代码语言:javascript
复制
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");

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-28 02:13:24

代码语言:javascript
复制
Sleep(15000 - timeTakenForCurlInMs);
票数 2
EN

Stack Overflow用户

发布于 2011-02-28 02:28:49

这是一个愚蠢的黑客攻击,每秒都会失败。它是基于POST教程。我破解了GET的东西,它工作得很好。

代码语言:javascript
复制
  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;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5133968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档