FtpGetFile WinINEt 永不返回

FtpGetFile WinINEt never returns

本文关键字:返回 WinINEt FtpGetFile      更新时间:2023-10-16

我遇到了一个奇怪的问题(很奇怪,让我说呵呵)。在 EXE 文件 (24 MB) 的 FTP 下载期间,如果连接中断,则似乎是 WinINEt 库的函数 FtpGetFile 存在错误,并且永远不会返回。这会导致将来的文件传输失败(连接已打开)。显然,我通过增加服务器传输的超时找到了解决方法,但我不喜欢它。我没有通过谷歌搜索发现类似的问题(也许我引入了错误的关键字)。

我在互联网上阅读了一些论坛,似乎每个人都不建议使用 FtpGetFile,因为它有问题。

这出现在具有很大滞后(并非总是如此)的网络场景中,但在良好条件下它会消失(下载正确进行并且 FtpGetFile 始终返回)。

以下是我如何使用该功能:

if( FtpGetFile(m_hFtpSession, strSourcePath.c_str(), strTargetPath.c_str(), 0, 0, FTP_TRANSFER_TYPE_BINARY, 0)==TRUE)

谁能证实这一点?我应该重构我的代码并寻找更新吗?

谢谢

我找到了一种不使用FtpGetFile下载文件的方法。我希望这段代码可以帮助某人:

bool RetrieveFile(const string& strSource, const string& strTarget) {
   /* The handle for the transfer */
   HINTERNET hTransfer = NULL;
   /*
    * Set default error
    */
   DWORD error = ERROR_SUCCESS;
   if( !isConnected ) {
     debug("%s(): ERROR not connectedn", __FUNCTION__);
     return false;
   }
   /* Initiate access to a remote FTP connection */
   hTransfer = FtpOpenFile(hFtpSession, strSource.c_str(), GENERIC_READ,
       FTP_TRANSFER_TYPE_BINARY, 0);
   if(hTransfer) {
      std::ofstream myostream(strTarget.c_str(), std::ios::binary);
      if ( myostream.is_open() ) {
         static const DWORD SIZE = 1024;
         BYTE data[SIZE];
         DWORD size = 0;
         do {
            BOOL result = InternetReadFile(hTransfer, data, SIZE, &size);
            if ( result == FALSE ) {
               error = GetLastError();
               Debug("InternetReadFile(): %lun", error);
            }
            myostream.write((const char*)data, size);
         }
         while ((error == ERROR_SUCCESS) && (size > 0));
         // Close the stream
         myostream.close();
      }
      else {
         Debug("Could not open '%s'.n", strTarget.c_str());
         error = ERROR_FILE_NOT_FOUND; // Not necessarily not found, but it is to describe a file error which is different from ERROR_SUCCESS
      }
      // Close
      const BOOL result = InternetCloseHandle(hTransfer);
      if ( result == FALSE ) {
         const DWORD error = GetLastError();
         debug("InternetClose(): %lun", error);
      }
      /* Check error status of the process */
      return (error == ERROR_SUCCESS);
   }
   DWORD dwInetError;
   DWORD dwExtLength = 1000;
   TCHAR *szExtErrMsg = NULL;
   TCHAR errmsg[1000];
   szExtErrMsg = errmsg;
   int returned = InternetGetLastResponseInfo( &dwInetError, szExtErrMsg, &dwExtLength );
   debug("dwInetError: %d  Returned: %dn", dwInetError, returned);
   debug("Buffer: %sn", szExtErrMsg);
   debug("%s() : ERROR to get '%s' file (errorCode=%d)n", __FUNCTION__, strSource.c_str(), GetLastError());
   return false;
}