cURL
Haxx ad
libcurl

curl's project page on SourceForge.net

Sponsors:
Haxx

cURL > Mailing List > Monthly Index > Single Mail

curlpp mailing list Archives

[cURLpp] Multithreading error

From: Michael Hecker <Hecker.Michael_at_gmx.de>
Date: Wed, 03 Oct 2007 19:20:09 +0200

Hello,
I am new to this mailinglist and from Germany.
So sorry for my bad English and thank you very much for your help!

I wrote a MFC program with Visual C++ 6.0 (I know its an old version).
In the program I have got six threads, which download information via
HTTP and HTTPS (GET and POST + Cookies) from different webservers.
At the beginning I were really happy with cURL, but when I send my
program into a longtime test, I got an "accessviolation error" in
libeay32.dll

I'm sending you my class as an attachment.

In the program I'm doing all the communication via one instance of the
class:
CCommunicator myCommunicator;

I tried an own instance for every thread but I got the same error.
I'm not getting the error when I'm doing all the communication in one
thread, but then everything is really slow.

Can the multi-interface solve my problem? How to do that? The code
examples didn't fit my needs.

Thank you very much in advance.
Michael

--
Communicator1.h
--
// Communicator1.h: Schnittstelle für die Klasse CCommunicator.
//
//////////////////////////////////////////////////////////////////////
#if 
!defined(AFX_COMMUNICATOR1_H__14D5C2D3_54BE_4E76_9A00_0A8A06FAAEF6__INCLUDED_)
#define AFX_COMMUNICATOR1_H__14D5C2D3_54BE_4E76_9A00_0A8A06FAAEF6__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "std_include.h"
#include "Stringmanager.h"
class CCommunicator 
{
public:
    CCommunicator();
    virtual ~CCommunicator();
    bool communicate(CString url, CString &quellcode, CString POST);
    bool execute(CString url, CString &quellcode, CString POST);
    int repeat_requests;
};
#endif // 
!defined(AFX_COMMUNICATOR1_H__14D5C2D3_54BE_4E76_9A00_0A8A06FAAEF6__INCLUDED_)
--
Communicator1.cpp
--
// Communicator1.cpp: Implementierung der Klasse CCommunicator.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Stringmanager.h"
#include "Communicator1.h"
    // Für Curl
    #include <errno.h>
    #include <time.h>
    #include <curl/curl.h>
    #include <curl/types.h>
    #include <curl/easy.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Konstruktion/Destruktion
//////////////////////////////////////////////////////////////////////
CCommunicator::CCommunicator()
{
    repeat_requests = 4;
}
CCommunicator::~CCommunicator()
{
}
//
// Curl: Reallocation of the memory
//
    void *myrealloc(void *ptr, size_t size)
    {
      if(ptr)    return realloc(ptr, size);
      else        return malloc(size);
    }
//
// Curl: Callback zum Speicern der Daten
//
    size_t WriteMemoryCallback(void *ptr, size_t size, size_t nmemb, 
void *data)
    {
      size_t realsize = size * nmemb;
      struct CCommunicator_MemoryStruct *mem = (struct 
CCommunicator_MemoryStruct *)data;
      mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize 
+ 1);
      if (mem->memory)
      {
        memcpy(&(mem->memory[mem->size]), ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
      }
      return realsize;
    }
bool CCommunicator::communicate(CString url, CString &quellcode, CString 
POST)
{
    bool res;
    int i=0;
    while(i<repeat_requests)
    {
        res = execute(url,quellcode,POST);
        if(!res)    i++;
        else        break;
    }
    return res;
}
//
// Curl: Send-Request
//
bool CCommunicator::execute(CString url, CString &quellcode, CString POST)
{
    quellcode = "";
    CURL                *curl_handle;
    CURLcode            result;
    struct                CCommunicator_MemoryStruct chunk;
    chunk.memory    =    NULL;
    chunk.size        =    0;
    char                nline[256];
    bool rueckgabe    =    true;
    // Initiierung der Curl-Session
    curl_global_init(CURL_GLOBAL_ALL);
    curl_handle = curl_easy_init();
    if (curl_handle)
    {
    // Optionen der Session
    //
        // Einstellungen für https
        if(url.Find("https")!=-1)
        {
            curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 2);
        }
        // User-Agent bestimmen
        curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "myAgent");
        // URL, die abgefragt werden soll
        curl_easy_setopt(curl_handle, CURLOPT_URL, url);
        // Die Gesamte Kommunikation mit dem Server wird protokolliert
        // curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
        // This is not working. I never get the additional information 
in my callback function. Why?
        // Klasse soll alle Daten an WriteMemoryCallback senden
        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, 
WriteMemoryCallback);
        // Über gabe des "chunk" structs an die Callback-Funktion
        curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);
        // Cookie-Engine starten
        curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, "");
            // Alte Cookies löschen
            curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, "ALL");
        // POST - Daten
        if(POST!="")
        curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, POST);
    //
    ////
    // Cookies setzen
    //
        _snprintf(nline, 256,
            "Set-Cookie: myCookie=myData;"
            "expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; 
domain=.xyz.com");
   
        curl_easy_setopt(curl_handle, CURLOPT_COOKIELIST, nline);   
    //
    ////
   
    //
    // Anfrage ausführen
    //
        result = curl_easy_perform(curl_handle);
        if (result != CURLE_OK) {
            fprintf(stderr, "Curl perform failed: %s\n", 
curl_easy_strerror(result));
            quellcode.Format("Anfrage wurde nicht beantwortet.\r\nBitte 
überprüfen Sie die Internetverbindung!\r\n\r\nMeldung: 
%s",curl_easy_strerror(result));
            rueckgabe = false;
        }
    //
    // TMP: Ausgabe
    //
        if(quellcode=="" && rueckgabe)
        quellcode = chunk.memory;
    }
    else
    {
        quellcode = "curl_handle konnte nicht initiiert werden!\r\nBitte 
kontaktieren Sie den Support.";
        rueckgabe = false;
    }
    // 1. Ausnahmefehler
    if(quellcode.Find("The requested URL could not be retrieved")!=-1)
    rueckgabe = false;
   
    // 2. Ausnahmefehler
    if(quellcode.Find("Service Unavailable")!=-1)
    rueckgabe = false;
    //
    // Aufräumen
    //
        curl_easy_cleanup(curl_handle);
        curl_global_cleanup();
        if(chunk.memory)    free(chunk.memory);
    return rueckgabe;
}
_______________________________________________
cURLpp mailing list
cURLpp_at_rrette.com
http://www.rrette.com/mailman/listinfo/curlpp
Received on 2007-10-03

These mail archives are generated by hypermail.

donate! Page updated November 12, 2010.
web site info

File upload with ASP.NET