Develop FTP clients with Indy9

xiaoxiao2021-03-06  50

Develop FTP client with Indy9_05

- Realize the routine of FTP breakpoint resume and download speed limit

Roger yang

1. TIDFTP's GET () method implements the routine for breakpoint: procedure tMainform.directoryListBoxDBLClick (Sender: Tobject); var name {, line}: string; begin // If there is not connected to the FTP server, exit if Not NOT IdFTP1.Connected then exit; // Line: = DirectoryListBox.Items [DirectoryListBox.ItemIndex]; // get the file name name: = IdFTP1.DirectoryListing.Items [DirectoryListBox.ItemIndex] .FileName; // or file selected by the user is determined directory if IdFTP1.DirectoryListing.Items [DirectoryListBox.ItemIndex] .ItemType = ditDirectory then begin // Change directory // If a directory is selected, the process proceeds to the directory SetFunctionButtons (false); ChageDir (Name); SetFunctionButtons (true); end The file name on the ELSE BEGIN TRY / / server is the default file name of the Save Dialog. Savedialog1.FileName: = Name; // Determined whether the user has a single confirmation button in the Save Dialog. if Savedialog1.execute The Begin SetFunctionButtons (False ); // Transport IDFTP1.TRANSFERTYPE: = ftbinary; / / The number of bytes that need to be transmitted by default is the size of the file. Bytestotransfer: = idftp1.size (name); // Determine if the file exists if fileexists (name) If the file already exists, you have to ask the user whether it is a breakpoint, or overwritten. Case Messagedlg ('file aready exists Do you want to resume the download operation? ', MTConfirmation, mbyesnocancel, 0) of mryes: begin // If the transmission is transmitted, the number of bytes transmitted is the total number of files minus the transmitted bytes. Bytestotransfer: = bytestotransfer - FileSizebyName (Name); idftp1.get (name, savedialog1.filename, false, true); end; mrno: begin idftp1.get (name, savedialog1.filename, true);

Mrcancel: begin exit; end; end; end; end; end; end; end; end;

2. Calculate the transmission average speed (unit: kilobyte): Procedure TMAINFORM.IDFTP1WORK (Sender: Tobject; AWORKMODE: TWORKMODE; Const AWorkcount: Integer); var s: string; TotalTime: tdatetime; // Transfer time // RemainingTime: TDATETIME; H, M, Sec, MS: Word; DLTIME: DOUBLE; begin // The transfer time is equal to the current time minus transmission start time TOTALTIME: = now - stime; // Remove the transmitted time into Time, minute, second, millisecond decodetime (Totaltime, H, M, SEC, MS); // convert the transmitted time into second as a second as a unit sec: = sec m * 60 h * 3600; // millisecond as a decimal Some DLTIME: = Sec MS / 1000; if DLTIME> 0 Then aVerated: = {(AVOUNT / 1024) / DLTIME {) / 2}; if averaspeed> 0 The begin // According to the remaining bytes and The average speed, calculation of the remaining time sec: = trunc ((ProgressBar1.max - aworkcount) / 1024) / averagespeed; s: = format ('% 2D:% 2D:% 2D', [SEC DIV 3600, (SEC DIV) 60) MOD 60, SEC MOD 60]); s: = 'Time Remaining' S; END ELSE S: = ''; s: = formatfloat ('0.00 kb / s', aveeespeed ';' S;Case aworkmode of wmread: statusbar1.panels [1] .text: = 'Download Speed' S; WMWRITE: STATUSBAR1.PANELS [1] .text: = 'UPLOADE SPEED' S; End; if AbortTransfer dam.. Progressbar1.position: = aworkcount; AbortTransfer: = false; end; 3. Radio speed routine (Collection of indeed Demo) Function TMYDebugger.Recv (Var ABUF; Alen: Integer): Integer; var lwaittime: cardinal; LRECVTIME: Cardinal; Begin if Fbytespersecond> 0 THEN BEGIN LRECVTIME: = idglobal.gettickcount; result: = inherited Recv (abuf, alen);

LRECVTIME: = GetTickDiff (LRECVTIME, IDGLOBAL.GETTICKCOUNT); // Get the number of milliseconds required to transmit according to actual speed transmission, = (Result * 1000) Div fBytesecond; // Get counting the milliseconds required to transfer by specified speeds ipaittime>> LRECVTIME THEN BEGIN / / The actual transmission speed is faster than the specified transmission speed, the number of milliseconds of the SLEEP is idglobal.sleep (lwaittime - lrecvtime); END; Else Begin // The actual transmission speed is slower than the specified transmission speed, continues to transfer Result: = inherited Recv (ABuf, ALen); end; end; 4. a combination of speed and the average speed calculating routine: void __fastcall TfrmDllMain :: idFtpWork (TObject * Sender, TWorkMode AWorkMode, const int AWorkCount) {char strDebugString [1024 ]; Tdatetime dttotaltime; // The actual transfer time of the current file Word H, M, Sec, Msec; Double DTotalTime; // The actual transmission time of // current file is used as a unit long lcurrentfileTotalTime; // The current file is actually Transfer time, in milliseconds as unit long LrateTotalTime; // The current file is transmitted, the time transmitted, the time is transmitted, in milliseconds. / * MEMSET (STRDEBUGSTRING, 0, SIZEOF (SPRDebugstring); sprintf (strDebugstring, " - LStartTransfersize = [% d] ", LStartTra nsferSize); OutputDebugString (strDebugString); * / lTransferCompleted = lStartTransferSize AWorkCount; dtTotalTime = Now () - dtCurrentFileStartTime; // current actual file transfer time is equal to the current time minus the current file transmission start time DecodeTime (dtTotalTime, H, M , SEC, MSEC); // Time to remove the transmitted time, minute, second, millisecond sec = sec m * 60 h * 3600; dtotaltime = sec msec / 1000; // millisecond as a fractional part lcurrentfileTAlTime = Sec * 1000 msec; // as a unit / * MEMSET (strDebugstring, 0, sizeof (strDebugstring)); sprintf (strDebugstring, "--- aworkcount = [% d]", aworkcount;

OutputDebugString (strDebugString); memset (strDebugString, 0, sizeof (strDebugString)); sprintf (strDebugString, "--- lTransferCompleted = [% d]", lTransferCompleted); OutputDebugString (strDebugString); * / if (dTotalTime> 0) { DAVERAGESPEED = (DAVERAGESPEED AWORKCOUNT / DTOTALTIME) / 2;} // Limit the transfer speed. If the rated speed is 0, the speed of the speed IF (lbytespersec! = 0) {// calculates these transmissions bytes, how much time (unit: ms) lRatedTotalTime = ((lTransferCompleted - lStartTransferSize) / lBytesPerSec) * 1000; / * memset (strDebugString, 0, sizeof (strDebugString)); sprintf (strDebugString, "--- lRatedTotalTime = [% ld] ", lRatedTotalTime); OutputDebugString (strDebugString); memset (strDebugString, 0, sizeof (strDebugString)); sprintf (strDebugString," --- lCurrentFileTotalTime = [% ld] ", lCurrentFileTotalTime); OutputDebugString (strDebugString); * / / / Determining if the actual download speed is less than the rated download speed if (LratedTotaltime> lcurrentfileTotAltime) {// actual download speed is less than the rated download speed, the number of milliseconds of the SLEEP phase SEEEP (LrateTotalTime - lcurrentFileTotAltime);}} / * switch (aworkmode)} case wmRead: memset (strDebugString, 0, sizeof (strDebugString)); sprintf (strDebugString, "↓↓↓ download speed = [% ld]", (long) dAverageSpeed); OutputDebugString (strDebugString); break; case wmWrite: memset ( StrDebugstring, 0, sizeof (strDebugstring)); sprintf (strDebugstring, "

转载请注明原文地址:https://www.9cbs.com/read-82766.html

New Post(0)