cURL / Mailing Lists / curl-library / Single Mail

curl-library

CURL POST operation using read and write threads

From: Senthil Raja Velu <Senthil.Raja_at_ss8.com>
Date: Fri, 30 Jan 2009 19:02:23 -0800

Hi,
I am newbie to curl and trying to figure out how to use it with multi
threads. I need to perform a POST operation with XML content using a
multi threaded program. I need to perform the request operation in one
thread and read the response in another thread. I see the
curl_easy_perform does it all on one operation. But how do we make the
write operation in one thread and read operation in another. Any help is
greatly appreciated.

Here is the sample code snippet that I need to dissect between write and
read threads;

    curl = curl_easy_init();

    if(curl) {
        curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, my_trace);
        curl_easy_setopt(curl, CURLOPT_DEBUGDATA, &my_debug);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        curl_easy_setopt(curl, CURLOPT_URL, "http://www.server.com");
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
                curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L);
                curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,_errBuf);
                curl_easy_setopt(curl, CURLOPT_HEADER, 1L);

        res = curl_easy_perform(curl);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_FILE,(void *)&memStruct);
        curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION,
WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEHEADER,(void *)&httpHeader);

        _headerList = curl_slist_append(_headerList, "Content-Type:
text/xml");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, _headerList);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, requestLen);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
        curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 0L);

        if((retVal = curl_easy_perform(curl)) != 0)
        {
            printf("POST(): Error ! - %s error:%d retVal:%d\n",
                   _errBuf,errno,retVal);

            curl_slist_free_all(_headerList);
            curl_free(httpHeader.memory);
            curl_free(memStruct.memory);
            return -2;
        }

        printf("POST(): Received %d bytes\n", memStruct.size);
        printf("%s\n", memStruct.memory);

        curl_free(memStruct.memory);
        curl_slist_free_all(_headerList);
        curl_free(httpHeader.memory);

        curl_easy_cleanup(curl);
    }
Received on 2009-01-31