Board index » delphi » Sendin files from TClientSocket to a TServerSocket?

Sendin files from TClientSocket to a TServerSocket?

How do I send files from TClientSocket to a TServerSocket?

/Oskar

 

Re:Sendin files from TClientSocket to a TServerSocket?


lidholm <lidh...@bahnhof.se> schreef in berichtnieuws
eJaN5.136$zM2.6...@news.bahnhof.se...

Quote
> How do I send files from TClientSocket to a TServerSocket?

> /Oskar

**raw,untested code, just to put you on track**

procedure TForm1.ClientSocket1OnWrite( Socket: etc....);
var
 Stream : TMemoryStream:
begin
  Stream := TMemoryStream.Create;
  Stream.LoadFromFile('c:\dir\afile.dat');
  Stream.position := 0;
  if not Socket.SendStream( Stream ) then
     ShowMessage('could not send file');
  // note: do _NOT_ free stream. socket will do this!
end;

Note that at the server side, you probably will have to re-assemble the
data, since anything larger then a few Kb will arrive in separate packets,
which will fire multiple OnRead() events in a row. Therefore, it is a good
idea to prepend the data with the stream length:
Stream.Write( Stream.Size, SizeOf(Stream.Size) );

HTH,

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://www.claessens16.yucom.be
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

Re:Sendin files from TClientSocket to a TServerSocket?


Ok. That works but how do i recive and save it to disk? If I send a text
file i can open it in a memo using recive text but shoudent i use
ReciveBuffer or somthing like that. Im not very in to streams etc.

Quote
Dirk Claessens <will.bou...@back.com> wrote in message

news:3a05c89d$0$4491$73bec57d@news.be.uu.net...
Quote
> lidholm <lidh...@bahnhof.se> schreef in berichtnieuws
> eJaN5.136$zM2.6...@news.bahnhof.se...
> > How do I send files from TClientSocket to a TServerSocket?

> > /Oskar

> **raw,untested code, just to put you on track**

> procedure TForm1.ClientSocket1OnWrite( Socket: etc....);
> var
>  Stream : TMemoryStream:
> begin
>   Stream := TMemoryStream.Create;
>   Stream.LoadFromFile('c:\dir\afile.dat');
>   Stream.position := 0;
>   if not Socket.SendStream( Stream ) then
>      ShowMessage('could not send file');
>   // note: do _NOT_ free stream. socket will do this!
> end;

> Note that at the server side, you probably will have to re-assemble the
> data, since anything larger then a few Kb will arrive in separate packets,
> which will fire multiple OnRead() events in a row. Therefore, it is a good
> idea to prepend the data with the stream length:
> Stream.Write( Stream.Size, SizeOf(Stream.Size) );

> HTH,

> --
> Regards,

> Dirk Claessens
> ---------------------------------------------------------
> http://www.claessens16.yucom.be
> Attention: All spamshields raised; E-mails will bounce!
> ---------------------------------------------------------

Other Threads