cURL / Mailing Lists / curl-users / Single Mail

curl-users

Got "Couldn't connect to server" after running some time

From: Chung Pak Lai <ChungLai_at_lumenvox.com>
Date: Tue, 27 Mar 2007 12:14:15 -0700

Hi,
 
I am new to this Forum. I have been experienced a strange behavior with
libcurl after running a minute. My program is pretty simple, it
basically fetch a http document repeatly. However, after running a
minute or two, it starts complaining about "Couldn't connect to server".
And this problem ONLY happens on WINDOWS, I have ran the same program on
linux and it runs fine. Here is the piece of code that I am running. Am
I doing something wrong in my program? Thanks :
 
#include <stdio.h>
 
#include <curllib/curl.h>
 
//Data structure for cURL data
struct HTTPBuffer
{
 char *data;
 size_t size;
};
 
//Callback function for HTTPRead and cURL
size_t dummy_callback(void *ptr, size_t size, size_t nmemb, void *data)
{
 size_t realsize = size * nmemb;
    
 return realsize;
}
 
//used by LoadCompiledGrammarFromURL to read from http source into a
buffer
bool HTTPRead(const char* uri, char** buffer, unsigned long& buffsize)
{
 CURL *curl;
 CURLcode res;
 struct HTTPBuffer io_buf;
    io_buf.data = NULL;
    io_buf.size = 0;
 
 curl = curl_easy_init();
 
 if (curl)
 {
  curl_easy_setopt(curl, CURLOPT_URL, uri);
  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, dummy_callback);
  curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&io_buf);
  
  res = curl_easy_perform(curl);
 
  
  
  if (res)
  {
   curl_easy_cleanup(curl);
   printf("%Failure: %s\n", curl_easy_strerror(res));
   return false;
  }
  else
  {
   long http_code;
   CURLINFO info = CURLINFO_RESPONSE_CODE ;
   res = curl_easy_getinfo(curl, info, (long*)&http_code);
 
            curl_easy_cleanup(curl);
 
   if (res)
   {
    
    return false;
   }
   else
   {
    if (http_code >= 400)
    {
     return false;
    }
 
    return true;
   }
  }
 }
 else
 {
  return false;
 }
}
 
int main(int argc, char *argv[])
{
    curl_global_init(CURL_GLOBAL_ALL);
 
    while (true)
    {
        char *buf=NULL;
        unsigned long s;
        HTTPRead("http://192.168.0.103/dummy.txt", &buf, s);
        delete [] buf;
    }
 
    curl_global_cleanup();
    return 0;
}
 
Thanks
--Chung
 
Received on 2007-03-27