带有c和无限循环的libcurl

时间:2014-04-30 12:44:06

标签: c linux libcurl

#include <curl/curl.h>
#include <stdio.h>

int main(){

    while(1)
    {

        CURL *curl;
        CURLcode res;

        curl = curl_easy_init();

        if(curl) {

            curl_easy_setopt(curl, CURLOPT_URL, \
                             "http://192.168.0.120/test.php?r=09210&o=919292" );

            curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);

            res = curl_easy_perform(curl);

            curl_easy_cleanup(curl);       

        }

        sleep(15);

    }

    return 0;
}

此程序不会连续运行,它会在几天或几小时后停止使用值更新数据库。当然,我已经通过删除数据库中的数据并检查它是否正在更新来测试它。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

也许您可以尝试在while循环之外移动init和cleanup,以避免可能失败的不必要的操作。

#include <curl/curl.h>
#include <stdio.h>

int main(){
    CURL *curl;
    CURLcode res;

    curl = curl_easy_init();

    if(!curl) {
        return 1;
    }

    while(1)
    {
        curl_easy_setopt(curl, CURLOPT_URL, \
                             "http://192.168.0.120/test.php?r=09210&o=919292" );

        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);

        res = curl_easy_perform(curl);

        sleep(15);

    }

   curl_easy_cleanup(curl);       
   return 0;
}