 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
snake Guest
|
Posted: Sat Feb 11, 2006 3:03 pm Post subject: Transfer server to the client (with tcp componet) |
|
|
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
|
Posted: Sat Feb 11, 2006 3:03 pm Post subject: Re: Transfer server to the client (with tcp componet) |
|
|
i forgot : i use delphi 7 and this code was written with indy 9 component
thx |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Feb 13, 2006 9:03 am Post subject: Re: Transfer server to the client (with tcp componet) |
|
|
"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 |
|
 |
|
|
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
|
|