BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Transfer server to the client (with tcp componet)

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock
View previous topic :: View next topic  
Author Message
snake
Guest





PostPosted: Sat Feb 11, 2006 3:03 pm    Post subject: Transfer server to the client (with tcp componet) Reply with quote



hi

i can send a file from the client to the server, so now i need the contrary : send a file from the server to the client

look the code i use for sending file from client

Code:



// in the client

function SendFile(const AFileName: string;
  ATcpClient: TIdTCPClient) : Boolean;
  var
    Fs : TFileStream;
begin
  Result := False;
  ATcpClient.Connect(); //Connecte. Les propriétés Host et Port doivent être remplies.
  try
     Fs := TFileStream.Create(AFileName,fmOpenRead,fmShareDenyWrite); //Créer le flux
     try
        ATcpClient.WriteLn(Format('TRANS %s',[ExtractFileName(AFileName)])); //demander transfert
        try
          ATcpClient.WriteInteger(Fs.Size); //Ecrire la taille
          ATcpClient.WriteStream(Fs); //Ecrit le flux
        except
          MessageDlg('Erreur pendant l''envoi du fichier.', mtError, [mbOK], 0);
        end;
     finally
        FreeAndNil(Fs); //Libérer le flux
        Result := ATcpClient.ReadLn()='OK'; //OK uniquement si le serveur a renvoyé "OK"
     end;
  finally
     ATcpClient.Disconnect; //Déconnecter à la fin.
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  IdTCPClient.Host := 'localhost'; //Hôte
  IdTCPClient.Port := 1985; //Le serveur doit aussi écouter sur le port 1985
  if SendFile('c:\setup.exe',IdTCPClient) then
    MessageDlg('Ok !', mtInformation, [mbOK], 0)
  else
    MessageDlg('Erreur...', mtInformation, [mbOK], 0)
end;


// in the server

procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
  Line, FileName : String;
  i, FileSize : integer;
  Fs : TFileStream;
begin
    with AThread.Connection do
    try
        Line := ReadLn(); //Attends une commande de la forme TRANS suivi du nom de fichier
        i := Pos(' ',Line);
        if (i>0) and (LowerCase(Copy(Line,1,Pred(i)))='trans') then
        begin
           FileName := IncludeTrailingPathDelimiter(ExtractFilePath(Application.ExeName))
                            + Copy(Line,Succ(i),Length(Line)); //Copier nom de fichier
           Fs := TFileStream.Create(FileName,fmCreate); //Créer le flux
           try
             try
               FileSize := ReadInteger(); //Lire la taille
               ReadStream(Fs,FileSize,False); //Lire le flux
               WriteLn('OK'); //Signaler succès
             except
               WriteLn('ERR'); //Signaler une erreur
             end;
           finally
             FreeAndNil(Fs); //Libérer le flux dans tous les cas
           end;
        end
        else
          WriteLn('ERR'); //Commande incomprise
    finally
        Disconnect; //A la fin, on déconnecte
    end;
end;





thx
Back to top
snake
Guest





PostPosted: Sat Feb 11, 2006 3:03 pm    Post subject: Re: Transfer server to the client (with tcp componet) Reply with quote



i forgot : i use delphi 7 and this code was written with indy 9 component

thx
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Feb 13, 2006 9:03 am    Post subject: Re: Transfer server to the client (with tcp componet) Reply with quote



"snake" <prog_delphi (AT) hotmail (DOT) com> wrote in message
news:43edf408$1 (AT) newsgroups (DOT) borland.com...

Quote:
i can send a file from the client to the server, so now i
need the contrary : send a file from the server to the client

You already know how to transfer a file over a TIdTCPConnection, so what
EXACTLY are you having a problem with?

Quote:
Fs := TFileStream.Create(AFileName,fmOpenRead,fmShareDenyWrite);
//Créer le flux


TFileStream's constructor does not have 3 parameters. It only has two, ie:

Fs := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyWrite);

Quote:
ATcpClient.WriteInteger(Fs.Size); //Ecrire la taille
ATcpClient.WriteStream(Fs); //Ecrit le flux

WriteStream() can call WriteInteger() internally, so you can conbine those
two lines into a single statement:

ATcpClient.WriteStream(Fs, True, True, 0);

Quote:
Line := ReadLn(); //Attends une commande de la forme TRANS suivi
du nom de fichier
i := Pos(' ',Line);
if (i>0) and (LowerCase(Copy(Line,1,Pred(i)))='trans') then
begin

Rather then parsing the lines manually, you might consider using
TIdTCPServer's CommandHandlers collection instead to automate the
processing.

Quote:
FileSize := ReadInteger(); //Lire la taille
ReadStream(Fs,FileSize,False); //Lire le flux

ReadStream() can call ReadInteger() internally, so you can conbine those two
lines into a single statement:

ReadStream(Fs, -1, False);


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Internet Winsock All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.