curl / Mailing Lists / curl-library / Single Mail

curl-library

libcurl always gives error Could not resolve

From: Saurav Babu <saurav.babu_at_samsung.com>
Date: Thu, 8 Feb 2018 16:40:23 +0530

I'm using libcurl v7.53.1. I'm facing issue that libcurl is unable to resolve URL.
Sample app code is as below:

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

int main(void)
{
        CURL *curl;
        CURLcode res;
        int i;
        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        for (i = 0; i < 100; i++) {
                if(curl) {
                        res = curl_easy_perform(curl);
                        printf("count %d result %d\n", i, res);
                        if(res == CURLE_OK) {
                                long response_code;
                                curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                                printf("Response received is %ld\n", response_code);
                        }
                }
                sleep(1);
        }
        curl_easy_cleanup(curl);
        return 0;
}

I'm using this sample code in below scenario:
 1. Plug out ethernet cable
 2. Start Application
 3. Plug in ethernet cable
 4. Device comes in online state but libcurl still gives below error

* Added connection 9. The cache now contains 1 members
* STATE: CONNECT => WAITRESOLVE handle 0x48060; line 1455 (connection #9)
* Could not resolve: www.google.com (Could not contact DNS servers)
* Closing connection 9
* The cache now contains 0 members
* Expire cleared
count 9 result 6

When I change sample code as below to always initialize curl handle in for loop then libcurl successfully
resolves URL. Changed code is like below:

for (i = 0; i < 100; i++) {
        curl = curl_easy_init();
        curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        if(curl) {
                res = curl_easy_perform(curl);
                printf("count %d result %d test\n", i, res);
                if(res == CURLE_OK) {
                        long response_code;
                        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
                        printf("Response received is %ld\n", response_code);
                }
        }
        curl_easy_cleanup(curl);
        sleep(1);
}

Please let me know why libcurl is unable to resolve URL in my initial code.

Thanks,
Saurav
-------------------------------------------------------------------
Unsubscribe: https://cool.haxx.se/list/listinfo/curl-library
Etiquette: https://curl.haxx.se/mail/etiquette.html
Received on 2018-02-08