使用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;
}

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

你看看是客户端还是服务器端的休眠,应该就是这个原因,如果是服务器端,定时器访问,保持住连接,检查服务器有没有待机或者休眠的设置,关闭掉。