W3C home > Mailing lists > Public > www-lib@w3.org > October to December 2002

using threads in libwww

From: <mnautiyal@hss.hns.com>
Date: Tue, 8 Oct 2002 20:51:50 +0530
To: www-lib@w3.org
Message-ID: <65256C4C.00546CAD.00@sandesh.hss.hns.com>


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,

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 {
            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");

    /* We are done with this request */

    /* Terminate libwww */


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

#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");
 sleep (20);


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 */

    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);
    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

This archive was generated by hypermail 2.3.1 : Tuesday, 6 January 2015 21:33:55 UTC