cURL / Mailing Lists / curl-library / Single Mail

curl-library

Problem with my class in independents threads- Error: "Failure when receiving data from the peer"

From: Denis Bondarev <asterganster_at_gmail.com>
Date: Mon, 27 Jul 2009 01:17:27 +0300

Hello. My name is Denis. I am programming on Builder6 C++ and use dinamic
libcurl (libcurl.dll). I have a problem, that hurts me very many time.
I write a class on my Builder 6 C++ that here full text:

#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/types.h>

class GCurl
{
//

private:
 HINSTANCE cl ;

CURLcode res;

  CURL*(__stdcall *curl_easy_init)();
CURLcode(__stdcall *curl_easy_setopt )(CURL *curl, CURLoption option, ...);
CURLcode (__stdcall *curl_easy_perform )(CURL *curl);
CURLcode(__stdcall *curl_easy_getinfo )(CURL *curl, CURLINFO info, ...);
void (__stdcall *curl_easy_cleanup )(CURL *curl);
struct curl_slist *(__stdcall *curl_slist_append )(struct curl_slist
*list,const char *string);
void (__stdcall *curl_slist_free_all)(struct curl_slist * list);
 void (__stdcall *curl_global_cleanup)();

public:
 AnsiString GCurl_Error;
   FuncHTTPGet();
    Init();

 static size_t Writer(char *data, size_t size, size_t nmemb, AnsiString
*buffer)
{
      size_t result = 0;

      if(buffer != NULL)
      {
          buffer->Insert(data,buffer->Length()+1);
          result = size * nmemb;
      }

       //delete data;

      delete &nmemb;

      return result;
     delete &result;
     delete buffer;
      } ;

int GCurl::Init(int r)
 {
 cl = NULL;
 if( ( cl = LoadLibrary( "libcurl.dll" ) ) == NULL )
    MessageBox(NULL, "I can't load libcurl","ERROR", 0);
 curl_easy_init =(CURL*(__stdcall*)())GetProcAddress(cl, "curl_easy_init" );
curl_easy_setopt =(CURLcode(__stdcall *)(CURL *curl, CURLoption option,
...))GetProcAddress(cl, "curl_easy_setopt");
curl_easy_perform =(CURLcode(__stdcall *)(CURL *curl))GetProcAddress(cl,
"curl_easy_perform" );
curl_easy_cleanup =(void (__stdcall *)(CURL *curl))GetProcAddress(cl,
"curl_easy_cleanup" );
curl_easy_getinfo=(CURLcode(__stdcall *)(CURL *curl, CURLINFO info,
...))GetProcAddress(cl, "curl_easy_getinfo");
curl_slist_append=(curl_slist*(__stdcall *)(struct curl_slist *list,const
char *string))GetProcAddress(cl,"curl_slist_append");
curl_slist_free_all=(void(__stdcall *)(struct curl_slist
*list))GetProcAddress(cl,"curl_slist_free_all");
curl_global_cleanup=(void(__stdcall *)())GetProcAddress(cl,
"curl_global_cleanup" );

 }

   AnsiString GCurl::FuncHTTPGet(int Time_Wait,AnsiString url)
 {
 char errbuf[CURL_ERROR_SIZE];
 CURL *curl;
 String table;
 GCurl_Error="Ok";
 curl = curl_easy_init();

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,&Writer ); // !

  curl_easy_setopt(curl, CURLOPT_WRITEDATA , &table);

  curl_easy_setopt(curl, CURLOPT_URL, url);

   curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
 curl_easy_setopt(curl, CURLOPT_TIMEOUT,Time_Wait);
 curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,1);
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "outfile.txt");
curl_easy_setopt ( curl, CURLOPT_COOKIEFILE, "outfile.txt" );

curl_easy_setopt(curl, CURLOPT_HEADER , 1 );

curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf);
res=curl_easy_perform(curl);
if(res)
GCurl_Error=errbuf;
 curl_easy_cleanup(curl);
curl_global_cleanup();

return table;
delete curl;
delete errbuf;
delete &table;
delete &url;
delete &Time_Wait;

  };
};

This class must just return HTTP-text of my URL. I cause his work in next
code from my general program:
....

GCurl*g= new GCurl();
g->Init(1);
RichEdit1->Text= (g->FuncHTTPGet(600,"http://www.yahoo.com/"));
 Memo1->Text=g->GCurl_Error;

....
After this: RichEdit1 have HTTP-text from URL and Memo1 have error-text.
It is real good work! But if I create two Threads for independents works and
in each thread I created new RichEdit,Memo and GCurl objects and run
this threads together - I have error: "Failure when receiving data from the
peer"!!!!! But why????? I created
 independents objects of my class with independents RichEdit and Memo
objects in independents threads! Why this error is true?????
Please help me solve this problem.

Best regards from me.
Denis.
Received on 2009-07-27