using threads in libwww

Hi,

There have been lot of discussions on about libwww being MT safe or
not. However I can't use it in even in a single Posix thread. When I create
the thread and call libwww API's on this thread, I get following error (as go
from trace mask on)

...
...
HTHost a54f8 going to state TCP_NEED_CONNECT.
HTHost a54f8 going to state TCP_ERROR.
Error....... Add  73    Severity: 1     Parameter: `Error 0'    Where: `connect'
DNS Delete.. object a4348 from list a7100
HTHost a54f8 going to state TCP_BEGIN.
Host connect Unlocking Host a54f8
....
....

Can you people tell, why the 'connect' error occured?
This works fine when I post data without creating any threads
Please advise how can I get the post successful using thread?


Thanks in advance,
Manoj


Complete code used for posting data is as following:
-------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <strings.h>
#include <fcntl.h>
#include "WWWLib.h"
#include "WWWInit.h"
#include <pthread.h>


HTChunk * chunk = NULL;
#define SAMPLE_DATA_FILE "xmlinput"
void *postData(void *arg);
int postData1 (unsigned char *buff , char* dst_url);


PUBLIC HTChunk * PostAnchorToChunk (HTParentAnchor *    source,
                         HTAnchor *             destination,
                         HTRequest *            request)

{
    if (source && destination && request) {
        HTChunk * chunk = NULL;
        HTStream * target = HTStreamToChunk(request, &chunk, 0);
        HTRequest_setOutputStream(request, target);
        if (HTPostAnchor(source, destination, request) != NULL)
            return chunk;
        else {
            HTChunk_delete(chunk);
            return NULL;
        }
    }
    return NULL;
}

PRIVATE int terminate_handler (HTRequest * request, HTResponse * response,
                               void * param, int status)
{
    char * string;
    string = HTChunk_toCString(chunk);
    if (string)
    printf("string : %s\n", string);
    else printf ("No response");
    HT_FREE(string);

    /* We are done with this request */
    HTRequest_delete(request);

    /* Terminate libwww */
    HTProfile_delete();

    exit(0);
}

void main()
{
  pthread_t      thr;
  pthread_attr_t  attr;
  FILE *fp;
  int i=0, c;
  unsigned char buff[10000];

  fp = fopen (SAMPLE_DATA_FILE,"r");
  if (!fp)
  {
        printf ("Can not open <%s>\n",SAMPLE_DATA_FILE);
        exit (0);
  }

  /* read xmlinput file */
  while ((c = getc(fp))!=EOF)
        buff[i++] = (unsigned char) c;
  buff[i] = '\0';

#if 0
postData1(buff, "http://tom.sine.com:7001/servlet/Spider");    <----  if
uncommented this works
#endif

#if 1
  pthread_attr_init(&attr);                                  <--- doesn't work
in a thread
  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  if(pthread_create(&thr, (pthread_attr_t *)&attr, postData, (void *)buff))
    fprintf(stderr, "failed to create a thread\n");
  pthread_attr_destroy(&attr);
 sleep (20);
#endif

}

void *postData(void *arg)
{
    unsigned char *buff = (unsigned char *) arg;

    printf ("Now going to call post data...\n");
    /* send data to HTTP client */
    postData1(buff, "http://tom.sine.com:7001/servlet/Spider");

}

int postData1 (unsigned char *buff , char *dst_url)
{
    HTRequest * request = NULL;
    HTParentAnchor * src = NULL;
    HTAnchor * dst = NULL;
    char dst_str[128];
    char data[10000];

    /* Create a new premptive client */
    HTProfile_newNoCacheClient("libwww-POST", "1.0");

    /* Add our own filter to update the history list */
    HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST);

    /* Set the timeout for long we are going to wait for a response */
    HTHost_setEventTimeout(10000); /* 10 seconds timeout */
    HTSetTraceMessageMask("sop");

    strcpy(dst_str, dst_url);
    strcpy (data,buff);

    if (data && *data && dst_str && *dst_str)
    {
        request = HTRequest_new();
        HTRequest_setOutputFormat(request, WWW_SOURCE);

        dst = HTAnchor_findAddress(dst_str);
        src = HTTmpAnchor(NULL);
        HTAnchor_setDocument(src, data);
        HTAnchor_setLength(src, strlen(data));

        chunk = PostAnchorToChunk(src, dst, request);
        HTEventList_loop(request);
    }
    return 0;
}


DISCLAIMER: This message is proprietary to Hughes Software Systems
Limited (HSS) and is intended solely for the use of the individual
to whom it is addressed. It may contain  privileged or confidential
information  and should not be circulated or used for any purpose other
than for what it is intended. If you have received this message in error,
please notify the originator immediately. If you are not the intended
recipient, you are notified that you are strictly prohibited from using,
copying, altering, or disclosing the contents of this message. HSS accepts
no responsibility for loss or damage arising from the use of the information
transmitted by this email including damage from virus.

Received on Tuesday, 8 October 2002 11:28:23 UTC