libcurl c,超时和成功转移

时间:2012-05-07 17:08:18

标签: c timeout libcurl

我必须使用FTP协议和c中的libcurl来传输文件。它工作正常,但我有一些问题。

1)如果传输开始,但在某一点我断开网络连接,我的程序仍然在libcurl函数中,速度变为0,我不能做任何其他事情。我试图设置超时(CURLOPT_TIMEOUT),但这只是传输时间的超时。

2)我遇到的第二个问题是,如果成功完成,我怎么知道转移?

我的传递代码是:

struct FtpFile {

  const char *filename;
  FILE *stream;
};
   long int size;

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream)
{
  struct FtpFile *out=(struct FtpFile *)stream;
  if(out && !out->stream) {
    /* open file for writing */
    out->stream=fopen(out->filename, "ab"); 
    if(!out->stream)
      return -1; /* failure, can't open file to write */
  }
  return fwrite(buffer, size, nmemb, out->stream);
}

int sz;

int main(void)
{
  CURL *curl;
  CURLcode res;
  char prova;

   struct stat statbuf;
   FILE *stream;

   /* apro il file per leggere la dimensione*/
   if ((stream = fopen("Pdf.pdf", "rb")) 
       == NULL)
   {
      fprintf(stderr, "Nessun file da aprire, il download partirà da zero.\n");
   }
   else
   {
   /* Ricevo informazioni sul file */
   fstat(fileno(stream), &statbuf);
   fclose(stream);
   size = statbuf.st_size;
   printf("Dimensione del file in byte: %ld\n", size);
   }

  struct FtpFile ftpfile={
    "Pdf.pdf", /* name to store the file as if succesful */
    NULL
  };

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();
  if(curl) {

    curl_easy_setopt(curl, CURLOPT_URL, "ftp://....");
    /* Define our callback to get called when there's data to be written */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
    /* Set a pointer to our struct to pass to the callback */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

    /* Switch on full protocol/debug output */
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); 

    /*Pass a long as parameter. It contains the offset in number of bytes that you want the transfer to start from.   
    Set this option to 0 to make the transfer start from the beginning (effectively disabling resume). For FTP, set  
    this option to -1 to make the transfer start from the end of the target file (useful to continue an interrupted 
    upload).

    When doing uploads with FTP, the resume position is where in the local/source file libcurl should try to resume 
    the upload from and it will then append the source file to the remote target file. */

    if(stream == NULL)
    {
        curl_easy_setopt(curl, CURLOPT_RESUME_FROM, 0);
    }
    else
    {
        curl_easy_setopt(curl, CURLOPT_RESUME_FROM, size);
    }

    /*Used to show file progress*/
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0);

    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);

    if(CURLE_OK != res) {
      /* we failed */
      fprintf(stderr, "curl told us %d\n", res);
    }
  }

  if(ftpfile.stream)
    fclose(ftpfile.stream); /* close the local file */

  curl_global_cleanup();

  return 0;
}

1 个答案:

答案 0 :(得分:1)

我最近回复了similar question

1)这是设计的。如果你想超时连接,请尝试使用这些:

curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, dl_lowspeed_bytes);    
curl_easy_setopt(curl, CURLOPT_LOW_SPEED_TIME, dl_lowspeed_time);

如果您的下载速率低于所需的阈值,则可以检查连接性和安全性。采取你认为合适的任何行动。

NB :在7.25.0中添加:CURLOPT_TCP_KEEPALIVE, CURLOPT_TCP_KEEPIDLE
所以这些可能是另一种合适的选择。


2)像这样:

curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &dl_bytes_remaining);
curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD, &dl_bytes_received);
if (dl_bytes_remaining == dl_bytes_received)
    printf("our work here is done ;)\n");