[SOLVED] Synapse (unit FtpSend) lazarus / 31-LCL ---------------------------------------------------------------------- Maverich · 2012-07-23 15:42 .... and the message has changed, see below ---------------------------------------------------------------------- BigChimp · 2012-07-23 15:54 Sorry, no reaction on the content of your question but could you place the code between code tags? Makes it easier to read and copy. Please also post your operating system+version and FPC & Lazarus version. See http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way _to_ask_questions_in_the_forum.3F thanks. ---------------------------------------------------------------------- Maverich · 2012-07-23 16:43 sorry, that's it the version Lazarus 1.1 - Fpc 2.7.1 (CodeTyphon 2.70) Os. Win7 64 bit ultimate - not on Virtual Machine first test Code: [Select]Result := FtpGetFile(Host, IntToStr(Port), Source, TargetFile, 'user', 'pwd'); Result := FtpPutFile(Host, IntToStr(Port), Source, TargetFile, 'user', 'pwd'); this function Send and Receive the file, but no information on bytes about sent and received i use the class TFTPSend Code: [Select]clFtp := TFTPSend.Create; //get file from FTP server   clFtp := TFTPSend.Create;   clFtp.BinaryMode := True;   clFtp.ChangeWorkingDir('/WA/Immagini');   clFtp.OnStatus :=   is correct the call ?   Result := clFtp.RetrieveFile('Delphi.jpg', False);  //from doc synapse : function RetrieveFile(const FileName: string; Restore: Boolean): Boolean; virtual; // the check unit FtpSend line 867 Code: [Select]if (FTPCommand('PASV') div 100) <> 2 then           Exit; how can I do ? Code: [Select]function TFTPSend.DataSocket: boolean; var   s: string; begin   Result := False;   if FIsDataTLS then     FPassiveMode := True;   if FPassiveMode then   begin     if FSock.IP6used then       s := '2'     else       s := '1';     if FSock.IP6used and not(FForceOldPort) and ((FTPCommand('EPSV ' + s) div 100) = 2) then     begin       ParseRemoteEPSV(FResultString);     end     else       if FSock.IP6used then         Exit       else       begin         // -----------------------------------------------         // ********** result <> 2 **********        // ------------------------------------------------         if (FTPCommand('PASV') div 100) <> 2 then           Exit;         ParseRemote(FResultString);       end;     FDSock.CloseSocket;     FDSock.Bind(FIPInterface, cAnyPort);     FDSock.Connect(FDataIP, FDataPort);     Result := FDSock.LastError = 0;   end   else   begin     FDSock.CloseSocket;     if FForceDefaultPort then       s := cFtpDataProtocol     else       s := '0';     //data conection from same interface as command connection     FDSock.Bind(FSock.GetLocalSinIP, s);     if FDSock.LastError <> 0 then       Exit;     FDSock.SetLinger(True, 10000);     FDSock.Listen;     FDSock.GetSins;     FDataIP := FDSock.GetLocalSinIP;     FDataIP := FDSock.ResolveName(FDataIP);     FDataPort := IntToStr(FDSock.GetLocalSinPort);     if FSock.IP6used and (not FForceOldPort) then     begin       if IsIp6(FDataIP) then         s := '2'       else         s := '1';       s := 'EPRT |' + s +'|' + FDataIP + '|' + FDataPort + '|';       Result := (FTPCommand(s) div 100) = 2;     end;     if not Result and IsIP(FDataIP) then     begin       s := ReplaceString(FDataIP, '.', ',');       s := 'PORT ' + s + ',' + IntToStr(FDSock.GetLocalSinPort div 256)         + ',' + IntToStr(FDSock.GetLocalSinPort mod 256);       Result := (FTPCommand(s) div 100) = 2;     end;   end; end; ---------------------------------------------------------------------- Maverich · 2012-07-23 20:27 Uses ... FtpSend,  typinfo, blcksock; with FtpGetFile(Host, IntToStr(Port), Source, TargetFile, user', 'pwd'); the file is transferred, but I need to know the bytes received (for progressbar ... if the file is big); the procedure DSockCallBack in never called. Code: [Select] TMyFTP = class(TFTPSend)   private     function DownloadFTP(URL, TargetFile: string): boolean;   protected     procedure DSockCallBack (Sender: TObject; Reason:  THookSocketReason; const  Value: string);   public     constructor Create;   published   end;      Code: [Select]constructor TMyFTP.Create; begin   inherited create;   DSock.OnStatus:=@DSockCallBack; end; Code: [Select]procedure TMyFTP.DSockCallBack(Sender: TObject; Reason: THookSocketReason;   const Value: string); var v: string; begin   if (reason=hr_writecount) then     v := getEnumName (typeinfo(THookSocketReason), integer(Reason)) + ' ' + Value;   Form1.Label1.Caption := v;   Form1.Label1.Refresh; end; Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin   clFtp := TMyFTP.Create; end; Code: [Select]function TMyFTP.DownloadFTP(URL, TargetFile: string): boolean; const   FTPPort=21;   FTPScheme='ftp://'; //URI scheme name for FTP URLs var   Host: string;   Port: integer;   Source: string;   FoundPos: integer;   i: integer; begin   clFtp.BinaryMode := True;   clFtp.ChangeWorkingDir('/WA/Immagini');   Host := 'ftp.webalice.it';   Source := '/WA/Immagini/readme_fr.htm';   Result := FtpGetFile(Host, IntToStr(Port), Source, TargetFile, user', 'pwd');   if result=false then ShowMessage('DownloadFTP: error downloading '+URL+'. Details: host: '+Host+'; port: '+Inttostr(Port)+'; remote path: '+Source+' to '+TargetFile); end; ---------------------------------------------------------------------- KpjComp · 2012-07-23 23:07 I've knocked up a quick example of doing FTP with Synapse and progressbar.  Ideally with Synapse been a blocking protocol it would probably work better inside a thread, I've done it here without.  The OnHeartBeat events of Synapse don't appear to get called enough on my machine to stop the GUI feeling a bit kludgey. ps: I'm not a synapse user so it might not be the best way, but it does appear to work. ---------------------------------------------------------------------- ludob · 2012-07-24 05:53 Setting the block size (64k default) smallerCode: [Select]ftp.DSock.SendMaxChunk:=8192; and adding a Code: [Select]application.ProcessMessages;to TForm1.OnMonitor will make the app snappier, if needed. On fast connections you won't see the difference and the send speed can actually drop. ---------------------------------------------------------------------- KpjComp · 2012-07-24 06:36 @ludob: I did think about changing the SendMaxChunk but found that idea a little clunky, as if the internet was having a bad hair day, you'll still get the issue no matter what the chunk size was And the Application.ProcessMessages is what I'm already doing, I've got them on the OnHeartBeat, I also had on the OnMonitor but that made no difference. That's why in the end I came to the conclusion using Threads seemed better. ---------------------------------------------------------------------- Maverich · 2012-07-24 07:52 Code: [Select]  type     THookSocketStatus = procedure(Sender: TObject; Reason: THookSocketReason; const Value: string) of object;     { Callback }     Callback = class     class procedure Status (Sender: TObject; Reason: THookSocketReason; const Value: String);   end; Code: [Select]{ Callback } class procedure Callback.Status(Sender: TObject; Reason: THookSocketReason;   const Value: String); var v: String;   begin     v := getEnumName (typeinfo(THookSocketReason), integer(Reason)) + ' ' + Value;     Form1.Memo1.Lines.Add(v);     if (reason=hr_writecount) then  inc(SizeOUT,StrToInt64Def(Value,0));     Form1.Label1.Caption:=inttostr(SizeOUT);    Application.ProcessMessages; // <-added end; Synapse example http://www.ararat.cz/synapse/doku.php/public:howto:onstatus   FTPS.DSock.OnStatus := CallBack.Status; unit1.pas(226,42) Error: Wrong number of parameters specified for call to "Status"   where is the correct syntax ? from Synapse example ....   attaching this callback to a socket:   Sock := T[UDP/TCP/..]BlockSocket.Create;   Sock.OnStatus := callback.Status; I do not understand this  -> Sock := T[UDP/TCP/..]BlockSocket.Create; Code: [Select]procedure TForm1.FormCreate(Sender: TObject); begin   FTPS:= TFTPSend.Create; end; execute the FtpGetFile and assign event OnStatus FTPS.DSock.OnStatus := CallBack.Status; Code: [Select]procedure TForm1.Button3Click(Sender: TObject); var   bEsito: boolean;   Source, TargetFile: string;   Reason: THookSocketReason; begin   SizeIN:=0;   SizeOUT:=0;   FTPS.TargetHost:= 'ftp.webalice.it';   FTPS.TargetPort := '21';   FTPS.UserName := 'user';   FTPS.Password := 'pwd';   FTPS.DSock.SendMaxChunk := 8192;  //<-- added   if not FTPS.Login then Form1.Caption:= 'Not Connected';   FTPS.DSock.OnStatus := CallBack.Status;   Source := '/WA/Immagini/readme_fr.htm';   TargetFile := 'D:\Temp\Test\Messaggi.jpg';   Result := FtpGetFile('ftp.webalice.it', '21', Source, TargetFile, 'user', 'pwd'); end; ---------------------------------------------------------------------- KpjComp · 2012-07-24 09:01 @Maverich: Your still calling FtpGetFile(,  that takes away the whole reason of creating a TFTPSend object. Check out the Demo I made, that works.  If you want me to modify to use threads, let us know.  I didn't use threads to keep things simple so you could follow. edit:  Just noticed you appear to want to retrieve a file, my demo was for sending.  But with some small modifications you should be able to change for retrieving. ---------------------------------------------------------------------- Maverich · 2012-07-24 10:13 thank you very much I tried your example, and I'll better understand my mistakes; I also tried the inverse, RetrieveFile, but it raises the exception Unable to OpenFile Code: [Select]      if not ftp.Login then         raise Exception.Create('could not login');       //destination File       SetCurrentDir('D:\Temp\Test');       //change directory ... the file exists       if ftp.ChangeWorkingDir('/WA/') then         begin          ftp.RetrieveFile('project1.zip', false);         end;       ftp.Logout; ---------------------------------------------------------------------- KpjComp · 2012-07-24 10:22 Hi @Maverich. You've not given me much to work on there, are you able to tell me at what point you get the unable to open file. One small point, if your wanting to have progressbar for retrieving, you will want get the ftp.FileSize and you will need to retrieve this after the ftp.login command. ---------------------------------------------------------------------- KpjComp · 2012-07-24 10:39 Here's a modified version for retrieving a file. ---------------------------------------------------------------------- Maverich · 2012-07-24 12:41 Thanks for Everything; I was even synapse user, I used Indy 10 (10.2.0.30) with CodeTyphon 2.40, CodeTyphon 2.70 with Indy 10 (rel. higher) and has problems with IdTCPClient IdFTP.