⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multiftp.cpp

📁 使用vc++6.0开发的一个支持多线程ftp程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <basepch.h>

#pragma hdrstop

#include "MultiFtp.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//

static inline void ValidCtrCheck(TMultiFtp *)
{
        new TMultiFtp(NULL);
}
//---------------------------------------------------------------------------
__fastcall TMultiFtp::TMultiFtp(TComponent* Owner)
        : TComponent(Owner)
{
   lock = false;
   isUseFile = false;
   runningThreadCnt = 0;
   stop = false;
   this->Owner  = Owner;
}
__fastcall TMultiFtp::~TMultiFtp()
{
    fclose(this->globalFile);
    if(this->inforImpl.fromToImpl)
      delete[] this->inforImpl.fromToImpl;
}
//---------------------------------------------------------------------------
namespace Multiftp
{
        void __fastcall PACKAGE Register()
        {
                 TComponentClass classes[1] = {__classid(TMultiFtp)};
                 RegisterComponents("System", classes, 0);
        }
}
//---------------------------------------------------------------------------
void __fastcall TMultiFtp::FreeMemory()
{
    if(this->globalFile)
       fclose(this->globalFile);
    if(this->inforImpl.fromToImpl)
      delete[] this->inforImpl.fromToImpl;
}
SOCKET __fastcall TMultiFtp::ConnectFtp(String host ,int port ,String userName ,String  pass)
{
    this->DoOnTextOut("欢迎使用funinhand多线程,断点续传软件!!");
    MultiThreadDealSocket *dealSocket = new MultiThreadDealSocket();
    SOCKET client = dealSocket->GetConnect(host,port);
    char * buffer = new char[100];
    int recLen ;
    recLen = recv(client,buffer,100,0);
    buffer[recLen]=0;
    if(client == NULL)
    {
      this->DoOnException("连接ftp服务器失败!");
      delete[] buffer;
      return NULL;
    }
    this->DoOnTextOut("连接ftp服务器成功!");
    String user = "USER  "+userName+" \r\n";
    this->DoOnTextOut(user);
    send(client,user.c_str(),user.Length(),0);
    recLen = recv(client,buffer,100,0);
    buffer[recLen]=0;
    if(GetCode(buffer) == "331")
    {
       this->DoOnTextOut("服务器要求验证密码。");
       String password = "PASS "+pass+" \r\n";
       this->DoOnTextOut(password);
       send(client,password.c_str(),password.Length(),0);
       recLen = recv(client,buffer,100,0);
        buffer[recLen]=0;
       int tryTimes = 3;
       while(GetCode(buffer) != "230" && tryTimes > 0)
       {
          send(client,password.c_str(),password.Length(),0);
          recLen = recv(client,buffer,100,0);
           buffer[recLen]=0;
          tryTimes --;
          this->DoOnTextOut("第"+IntToStr(3-tryTimes)+"尝试");
       }
       if(tryTimes < 0)
       {
           this->DoOnException(userName +"登录失败!");
           delete[] buffer;
           return NULL;
       }
       else
          this->DoOnTextOut("登录成功!");
    }
    char *type = "TYPE I \r\n";
    this->DoOnTextOut(type);
    send(client,type,strlen(type),0);
    recLen = recv(client,buffer,100,0);
     buffer[recLen]=0;
    this->DoOnTextOut(buffer);
    delete[] buffer;
    return client;
}
void __fastcall TMultiFtp::SetStop(bool stop)
{
  this->stop  = stop;
}
void __fastcall TMultiFtp::DoOnComplete()
{
  if(this->FOnComplete)
     this->FOnComplete(this->Owner);
}
void __fastcall TMultiFtp::DoOnGetFileSize(DWORD fileSize)
{
   if(this->FOnGetFileSize)
      this->FOnGetFileSize(this->Owner,fileSize);
}
void __fastcall TMultiFtp::DoOnTextOut(String text)
{
 if(this->FOnTextOut)
  {
     int index = text.Pos("\r\n");
     if(index > 0)
       this->FOnTextOut(this->Owner,text.SubString(1,index-1));
     else
       this->FOnTextOut(this->Owner,text);
  }
}
void __fastcall TMultiFtp::DoOnException(String error)
{
   if(this->FOnException)
   {
      int index = error.Pos("\r\n");
      if(index > 0)
      {
        this->FOnException(this->Owner,error.SubString(1,index-1));
      }
      else
      {
          this->FOnException(this->Owner,error);
      }
   }
}
void __fastcall TMultiFtp::DoOnProgress(DWORD pos)
{
  if(this->FOnProgress)
    this->FOnProgress(this->Owner,pos);
}
void  __fastcall TMultiFtp::DoOnGetRate(DWORD cnt)
{
   if(this->FOnGetRate)
      this->FOnGetRate(this->Owner,cnt);
}
String __fastcall TMultiFtp::GetCode(String revStr)
{
  String str;
  int index ;
  index = revStr.Pos(" ");
  str = revStr.SubString(1,index-1);
  return str;
}
DWORD __fastcall TMultiFtp::GetFileSize()
{
   return this->fileSize;
}
DWORD __fastcall TMultiFtp::GetFtpFileSize(SOCKET client ,String fileName)
{
   String  size = "SIZE "+this->FFileName+" \r\n";
   this->DoOnTextOut(size);
   send(client,size.c_str(),size.Length(),0);
   char *buffer= new char[100];
   int recLen ;
   recLen = recv(client,buffer,100,0);
   buffer[recLen]=0;
   this->DoOnTextOut(buffer);
   if(GetCode(buffer) != "213")
   {
     this->DoOnTextOut("尝试第二次获取");
     send(client,size.c_str(),size.Length(),0);
     recLen = recv(client,buffer,100,0);
      buffer[recLen]=0;
      this->DoOnTextOut(buffer);
     if(GetCode(buffer) != "213")
     {
        this->DoOnTextOut("尝试第三次获取");
        send(client,size.c_str(),size.Length(),0);
        recLen = recv(client,buffer,100,0);
         buffer[recLen]=0;
         this->DoOnTextOut(buffer);
        if(GetCode(buffer) != "213")
        {
            this->DoOnException("获取文件大小失败!");
            delete[] buffer;
             return 0;
        }
        else
        {
            this->DoOnTextOut(buffer);
        }
     }
     else
     {
         this->DoOnTextOut(buffer);
     }
   }
   else
   {
       this->DoOnTextOut(buffer);
   }
   String recvStr(buffer);
   delete[] buffer;
   int index =  recvStr.Pos("\r\n");
   recvStr = recvStr.SubString(1,index-1);
   index = recvStr.Pos(" ");
   recvStr = recvStr.SubString(index+1,recvStr.Length()- index);
   this->fileSize  =StrToInt(recvStr);
   return this->fileSize;
}
bool __fastcall  TMultiFtp::CheckIsSupportMulti(SOCKET client)
{
   String rest = "REST 100 \r\n";
   char *buffer = new char[100];
   this->DoOnTextOut(rest);
   int recLen ;
   send(client,rest.c_str(),rest.Length(),0);
   recLen = recv(client,buffer,100,0);
   buffer[recLen]=0;
   this->DoOnTextOut(buffer);
   if(GetCode(buffer) != "350")
   {
       delete[] buffer;
       return false;
   }
   delete[] buffer;
   return true;
}
FileTail * __fastcall TMultiFtp::GetFilePosLen()
{
   while(lock)
   {
     Sleep(50);
   }
   lock = true;
   FileTail * temp  = new FileTail[1];
   if(this->currentFilePos >= fileSize)
   {
       lock = false;
       temp->isOk = true;
       return temp;
   }
   if(this->FPerGetLen + this->currentFilePos > fileSize)
   {
       temp->pos = ConvertIntToStr(this->currentFilePos);
       temp->len = ConvertIntToStr(this->fileSize - this->currentFilePos);
       temp->isOk = false;
       this->currentFilePos = fileSize;
       lock = false;
       return temp;
   }
   temp->pos = ConvertIntToStr(this->currentFilePos);
   temp->len = ConvertIntToStr(this->FPerGetLen);
   temp->isOk = false;
   this->currentFilePos = this->currentFilePos + this->FPerGetLen;
   lock = false;
   return  temp;
}
String __fastcall TMultiFtp::ConvertIntToStr(DWORD len)
{
     String str;
     str = IntToStr(len);
     int strLen;
     strLen = str.Length();
     for(int i =0 ; i < 10 -strLen ; i++)
     {
        str = "0"+str;
     }
     return str;
}
bool __fastcall TMultiFtp::ChangeDirectory(SOCKET client ,String dirName)
{
    String dir =  "CWD "+dirName+" \r\n";
    this->DoOnTextOut(dir);
    char *buffer = new char[100];
    int recLen ;
    send(client,dir.c_str(),dir.Length(),0);
    recLen = recv(client,buffer,100,0);
    buffer[recLen]=0;
    if(GetCode(buffer) != "250")
    {
        delete[] buffer;
        this->DoOnException(buffer);
        return false;
    }
    this->DoOnTextOut(buffer);
    delete[] buffer;
    return true;
}
void __fastcall TMultiFtp::StartDownloadFile()
{
   SOCKET client = this->ConnectFtp(this->FHost,this->FPort,this->FUserName,this->FPass);
   this->DoOnTextOut("下载的线程模块数为:"+IntToStr(this->FThreadCnt));
   if(!client)
   {
       return;
   }  //连接ftp成功
   if(!this->CheckIsSupportMulti(client))
   {
      this->DoOnTextOut("该站点不支持断点续传。");
      this->FThreadCnt =  1;     //如果ftp服务器不支持多线程下载
   }
   this->DoOnTextOut("该站点支持断点续传。");
   ////////////将当前的工作目录设置成
   this->FileName = this->SetCurrentDir(client,this->FilePath);
   this->GetFtpFileSize(client,this->FileName);
   this->DoOnGetFileSize(this->fileSize);  //输出文件大小
   if(this->fileSize == 0)
   {
      closesocket(client);
      return;
   }
   if(this->fileSize <= (DWORD)this->PerGetLen)
   {
      this->FThreadCnt = 1;
   }
   if(this->FileName == "")
   {
      closesocket(client);
      return;
   }
   String hisFileName = this->FileName + ".san";
   ///////////////////检测是否断点续传/////////////////////////
   if(FileExists(hisFileName)&& (GetFileSizeByName(hisFileName) > 0)) //如果文件存在
   {
      this->GetInfor(hisFileName);
   }
   else
   {
     globalFile = fopen(hisFileName.c_str(),"w+b");
     if(globalFile == NULL)
     {
      this->DoOnException("创建文件"+hisFileName+"失败");
      closesocket(client);
          return ;
     }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -