Advertisement
Guest User

Untitled

a guest
Aug 11th, 2016
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1.  
  2. UNIT TesterForm;
  3.  
  4. INTERFACE
  5.  
  6. USES
  7. WinApi.Windows, System.SysUtils, System.Classes, Vcl.StdCtrls, VCL.Forms, Vcl.Controls, Vcl.Samples.Spin, Vcl.ComCtrls,
  8. cRichEdit, cLog, IdComponent, IdBaseComponent, IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdFTP;
  9.  
  10.  
  11. TYPE
  12. TfrmTester = class(TForm)
  13. Button1: TButton;
  14. Log: TRichLog;
  15. FTP: TIdFTP;
  16. procedure Button1Click(Sender: TObject);
  17. procedure FTPDisconnected(Sender: TObject);
  18. procedure FTPConnected(Sender: TObject);
  19. procedure FTPStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
  20. private
  21. public
  22. end;
  23.  
  24. VAR
  25. frmTester: TfrmTester;
  26.  
  27. IMPLEMENTATION {$R *.dfm}
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. {-----------------------------------------------------------------------------------------------------------------------
  39. DOWNLOAD PROGRESS
  40. -----------------------------------------------------------------------------------------------------------------------}
  41. procedure TfrmTester.FTPConnected(Sender: TObject);
  42. begin
  43. Log.AddInfo('Connected');
  44. Log.Update;
  45. end;
  46.  
  47.  
  48.  
  49. procedure TfrmTester.FTPDisconnected(Sender: TObject);
  50. begin
  51. Log.AddHint('Disconnected');
  52. Log.Update;
  53. end;
  54.  
  55.  
  56.  
  57. procedure TfrmTester.FTPStatus(ASender: TObject; const AStatus: TIdStatus; const AStatusText: string);
  58. begin
  59. Log.AddVerb(' > ' + AStatusText);
  60. Log.Update;
  61. end;
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. CONST
  70. MyFTP = 'ftp.ncbi.nlm.nih.gov';
  71. usr = 'anonymous';
  72. psw = 'anonymous';
  73. FtpBlastDbDir = '/blast/db/';
  74.  
  75.  
  76. function Connect2FTP_(FTP: TIdFTP; RemoteFolder: string; Log: TRichLog): Boolean;
  77. begin
  78. Result:= FTP.Connected;
  79. if NOT Result then
  80. begin { We are already connected }
  81. Log.AddVerb('Connecting to '+ MyFTP);
  82. Log.Update; { I need this for the case when the Internet connection is slow/broken }
  83.  
  84. FTP.Host := MyFTP;
  85. FTP.Username:= usr;
  86. FTP.Password:= psw;
  87.  
  88. TRY
  89. FTP.Connect; // <-------------- 'Read time out' here
  90. EXCEPT
  91. on E: Exception DO Log.AddError(E.Message); { Don't crash. Just show error on log }
  92. END;
  93.  
  94. Result:= FTP.Connected;
  95.  
  96. if Result
  97. then FTP.ChangeDir(RemoteFolder) { Go to DB folder on FTP }
  98. else Log.AddError('Error!');
  99. end;
  100. end;
  101.  
  102.  
  103.  
  104. procedure TfrmTester.Button1Click(Sender: TObject);
  105. VAR
  106. i: Integer;
  107. CONST
  108. FtpFile= 'nr.00.tar.gz.md5'; // ftp://ftp.ncbi.nlm.nih.gov/blast/db/nr.00.tar.gz.md5
  109. begin
  110. if Connect2FTP_(FTP, FtpBlastDbDir, Log) then
  111. for i:= 1 to 5 DO
  112. begin
  113. Connect2FTP_(FTP, FtpBlastDbDir, Log); { We try to connect before each download because the unpacking may take longer than 60 sec and the connection may get interrupted (Connection close greatfuly) }
  114.  
  115. if FTP.Connected
  116. then Log.AddVerb('Still connected to FTP.')
  117. else Log.AddVerb('Not connected to FTP.');
  118. Application.ProcessMessages;
  119.  
  120. { Download file }
  121. FTP.Get(FtpFile, ExtractFilePath(Application.ExeName)+ 'Downloaded.bin', TRUE, FALSE); //<--------- socket error 10038
  122.  
  123. Log.AddVerb('Download end. Going to sleep.');
  124. Application.ProcessMessages;
  125.  
  126. Sleep(62000);
  127. Application.ProcessMessages;
  128. end;
  129. end;
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement