LibCurl problem or WINAPI

May 18, 2011 at 10:57pm
Hello
I found a strange problem, i don't know if it has to do with libCurl or my code but evreytime i request from a URL the whole window just hangs until libCurl have buffred the site. I tried in console mode and it seems to be working.

For example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string outData;
 size_t w_data(void *buffer, size_t size, size_t nmemb, void *userp)
    {
      outData.append((char*)buffer, size*nmemb);
        return size*nmemb;
      } 

    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();  
         if(curl) {               
          curl_easy_setopt(curl, CURLOPT_URL, "http://edition.cnn.com/"); 
            curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,w_data);
              res = curl_easy_perform(curl);    
                /* always cleanup */ 
                 curl_easy_cleanup(curl);
          } 


Here is evreything

http://pastebin.com/PRQwwDzK
Last edited on May 18, 2011 at 10:59pm
May 19, 2011 at 9:03am
The curl_easy_perform function is synchronous (which means you can't continue until the function has completed one way or another)
May 19, 2011 at 10:40am
So nothing i can do beside change libary? Or maybe i just create a multithread :o

EDIT: Worked smoothly with a multithread :)
Last edited on May 19, 2011 at 1:12pm
May 19, 2011 at 4:51pm
Curl has a curl_multi_perform which does asynchronous (non-blocking) transfers I believe.
May 20, 2011 at 11:25am
Hi Mekolle, I have the same problem, but I can't understand how to use multitrhead, my English isn't very good and it's difficult to read the web page were ssl talk about it... Could you post some code? doesn't need to be anything special, just a little demonstration.

Thanks in advance.
May 20, 2011 at 12:19pm
AMCerasoli,

This code is only useable if you use WINAPI(windows.h) i dont know how BOOST and other libraries for multithreadning works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Function Declaration
DWORD WINAPI ThreadFn (LPVOID vpParam);

//How to call the thread
DWORD qThreadID; 
 HANDLE hThread = CreateThread(0, 0, ThreadFn, 0, 0, &qThreadID); 
 
  // Close it              
  CloseHandle(hThread);

// Function Definitions
DWORD WINAPI ThreadFn(LPVOID vpParam)
{
  // Your Code
	return 0;
}


http://www.youtube.com/watch?v=8C4wLzp5lK4
Last edited on May 20, 2011 at 1:51pm
May 20, 2011 at 1:37pm
But you had to use libCur with SSL right? I mean I'm using the simple version, using the pre-built binary (libcurl.dll). Or you just started a new thread and worked?.

I trying to install the pre-built binaries for OpenSSL from this website: http://www.shininglightpro.com/products/Win32OpenSSL.html But it says that I need Microsoft Visual C++ 2008 Redistributables. That means that I need to use VC++ or are some .dlls that I need to distribute along with my software later on?

Sorry for being so annoying... : (
May 20, 2011 at 2:02pm
I don't know anything about OpenSSL, I'm just using DeVC++ and downloaded http://curl.haxx.se/latest.cgi?curl=win32-devel-msvc, linked the library and all worked fine.
May 21, 2011 at 8:03am
Hm, I see, well, probably the MSVC already has that option pre-built.

But if you're using DevC++ why are you using the MSVC pre-compiled binaries? DevC++ doesn't use MinGW?

Since I read this from the libCur page I was confuse, well, actually I'm confuse.

Multi-threading Issues
The first basic rule is that you must never share a libcurl handle (be it easy or multi or whatever) between multiple threads. Only use one handle in one thread at a time.

libcurl is completely thread safe, except for two issues: signals and SSL/TLS handlers. Signals are used for timing out name resolves (during DNS lookup) - when built without c-ares support and not on Windows.

If you are accessing HTTPS or FTPS URLs in a multi-threaded manner, you are then of course using the underlying SSL library multi-threaded and those libs might have their own requirements on this issue. Basically, you need to provide one or two functions to allow it to function properly. For all details, see this:

OpenSSL

http://www.openssl.org/docs/crypto/threads.html#DESCRIPTION

GnuTLS

http://www.gnu.org/software/gnutls/manual/html_node/Multi_002dthreaded-applications.html

NSS

is claimed to be thread-safe already without anything required.

PolarSSL

Required actions unknown.

yassl

Required actions unknown.

axTLS

Required actions unknown.

When using multiple threads you should set the CURLOPT_NOSIGNAL option to 1 for all handles. Everything will or might work fine except that timeouts are not honored during the DNS lookup - which you can work around by building libcurl with c-ares support. c-ares is a library that provides asynchronous name resolves. On some platforms, libcurl simply will not function properly multi-threaded unless this option is set.

Also, note that CURLOPT_DNS_USE_GLOBAL_CACHE is not thread-safe.

Topic archived. No new replies allowed.