【问题标题】:libcurl c, timeout and success transferlibcurl c,超时和成功传输
【发布时间】:2012-05-16 04:46:55
【问题描述】:

我必须在 c 中使用 FTP 协议和 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;
}

【问题讨论】:

    标签: c timeout libcurl


    【解决方案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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-26
      • 1970-01-01
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多