cURL / Mailing Lists / curl-library / Single Mail


Re: Error: Cannot resolve localhost

From: weiping guo <>
Date: Mon, 14 Jan 2008 07:55:17 -0800 (PST)

Thanks, Daniel.

I found the bug in my program. It is kind of stupid. You are right, actullay the

 curl_easy_setopt(conn, CURLOPT_URL, url);

is in one function, and curl_easy_perform(conn); is in another function. Since url is a local variable, it is not accessible when perform is called.



----- Original Message ----
From: Daniel Stenberg <>
To: libcurl development <>
Sent: Friday, January 11, 2008 4:48:21 PM
Subject: Re: Error: Cannot resolve localhost

On Fri, 11 Jan 2008, weiping guo wrote:

> I have a very simple program to post some data to webpage in the localhost.

What libcurl version on what operating system? Do you build libcurl IPv6
enabled or with c-ares support?

> char url[REQUEST_LEN*2];

How big is REQUEST_LEN ?

> int n = sprintf(url, "http://%s:%d/%s?%s", this->ip_, this->port_,
> this->url_, request);
> code = curl_easy_setopt(conn, CURLOPT_URL, url);
> if( code != CURLE_OK )
> {
> return -1;
> }
> code = curl_easy_perform(conn);
> curl_easy_cleanup(conn);
> However, I got the error code CURLE_COULDNT_RESOLVE_HOST. This error is
> happened if this function is called for the first time. The subsequent calls
> would be all right.

That really makes no sense to me (if you're actually are using an IP address
in there as the code hints). I would rather guess that either you're not
showing us the exact code you're actually using here, or something in your
code is misbehaving memory wise or similar.

libcurl doesn't resolve plain IP addresses at all, so I don't think it can
even return that error on such an input.

When you speak of subsequent ones, do you mean that entire flow repeated
again? Since it closes the handle, all code will run the exact same way the
second time and thus I would say that indicates that the source of this
problem is outside of libcurl. (Assuming that you pass in a hostname then,
since when it isn't a hostname I can't explain the return code at all...)

> I do not know how to make it work for the first call. I am currently using
> an ugly workaround, ˙˙call this function in the constructor˙˙. Any help or
> suggestions are appreciated.

gdb or strace will of course help you figure out the root of this.

  Commercial curl and libcurl Technical Support:
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.
Received on 2008-01-14