cmd_clear 2020-07-27 09:42 采纳率: 66.7%
浏览 322
已结题

使用libcurl 将文件上传FTP服务器报,CURLE_COULDNT_CONNECT

1.问题背景
在需要每隔10分钟上传1张图片至远程FTP服务器。在上传间隔因功耗要求会进行休眠。上传了700多张之后。应用程序报错
与libcurl 的CURLE_COULDNT_CONNECT这错误有关,已经限制了产生time_wait的tcp连接的数量。应用程序依旧无法连接FTP服务器。
2.代码参考的是curl官方的实现改的

static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
  curl_off_t nread;
  /* in real-world cases, this would probably get this data differently
     as this fread() stuff is exactly what the library already would do
     by default internally */
  size_t retcode = fread(ptr, size, nmemb, stream);

  nread = (curl_off_t)retcode;

  fprintf(stderr, "*** We read %" CURL_FORMAT_CURL_OFF_T
          " bytes from file\n", nread);
  return retcode;
}


int ftpupload(char8 *localfile,char8 *remotefile)
{
  CURL *curl;
  CURLcode res;
  FILE *hd_src;
  struct stat file_info;
  curl_off_t fsize;
  char8 errmsg[100];

  char8 login[50];
  char8 interface[10];

  param_GetFtpServerUser(login,interface);

  struct curl_slist *headerlist = NULL;
  //static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS;
  //static const char buf_2 [] = "RNTO " RENAME_FILE_TO;

  /* get the file size of the local file */
  //printf("localfile:%s\n",localfile);
  if(stat(localfile, &file_info)) {
    sprintf(errmsg,"ftpupload: Couldn't open '%s': %s\n", localfile, strerror(errno));
    log_WriteCurrentMessage(errmsg);
    return 1;
  }
  fsize = (curl_off_t)file_info.st_size;

  printf("Local file size: %" CURL_FORMAT_CURL_OFF_T " bytes.\n", fsize);

  /* get a FILE * of the same file */
  hd_src = fopen(localfile, "rb");

  /* In windows, this will init the winsock stuff */
  curl_global_init(CURL_GLOBAL_ALL);

  /* get a curl handle */
  curl = curl_easy_init();
  if(curl) {
    /* build a list of commands to pass to libcurl */
    //headerlist = curl_slist_append(headerlist, buf_1);
    //headerlist = curl_slist_append(headerlist, buf_2);

    /* we want to use our own read function */
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);


    /* enable uploading */
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

    /* specify target */
    curl_easy_setopt(curl, CURLOPT_URL, remotefile);
    //curl_easy_setopt(curl, CURLOPT_UPLOAD_BUFFERSIZE,204800L);
    /* pass in that last of FTP commands to run after the transfer */
    //curl_easy_setopt(curl, CURLOPT_POSTQUOTE, headerlist);

    /* now specify which file to upload */
    curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);

    /* Set the size of the file to upload (optional).  If you give a *_LARGE
       option you MUST make sure that the type of the passed-in argument is a
       curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
       make sure that to pass in a type 'long' argument. */
    curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
                     (curl_off_t)fsize);

    curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, 1); //目录不存在时,上传文件时,先创建目录
    curl_easy_setopt(curl, CURLOPT_INTERFACE, interface);
    curl_easy_setopt(curl, CURLOPT_TIMEOUT, 60);
    curl_easy_setopt(curl, CURLOPT_USERPWD,login);

    //curl_easy_setopt(curl, CURLOPT_UPLOAD_BUFFERSIZE,120000L);
    tman_TaskReportSelf();

    /* Now run off and do what you've been told! */
    res = curl_easy_perform(curl);
    tman_TaskReportSelf();
    /* Check for errors */
    if(res != CURLE_OK)
    {
      sprintf(errmsg, "ftpupload: curl_easy_perform() failed: %s",curl_easy_strerror(res));
      log_WriteCurrentMessage(errmsg);
    }

    /* clean up the FTP commands list */
    //curl_slist_free_all(headerlist);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  fclose(hd_src); /* close the local file */

  curl_global_cleanup();
  if(res!= CURLE_OK)
  {
      return res;
  }
  return 0;
}
  • 写回答

2条回答 默认 最新

  • 浴火_凤凰 2020-07-27 10:58
    关注

    无法连接FTP服务器 有没有看到更详细的错误信息?
    或者懂网络通信的话 使用抓包看一下

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算