Solved

Send/Receive file with TClient/TServerSocket

Posted on 1998-09-12
1
337 Views
Last Modified: 2010-04-03
Hi, I would Like to know how to Send and receive files
with a TClientSocket and a TServerSocket?
0
Comment
Question by:mabell
1 Comment
 
LVL 35

Accepted Solution

by:
TimYates earned 50 total points
ID: 1339558
Try this:

Assuming :

   client : tClientSocket ;

(and you have opened the socket...)

procedure SendFile( filename : string ) ;
var
  f : FILE ;
  p : ^byte ;
  sz, osz, rsz : longint ;
begin
  AssignFile( f, filename );
  {$i-}Reset( f, 1 ){$i+}
  osz := FileSize( f ) ;
  p := allocMem( sizeof( byte ) * osz ) ;
  sz := osz ;
  rsz := BlockRead( f, p^, sz ) ;
  client.Socket.SendBuf( p^, rsz ) ;
  while( rsz < sz ) do
  begin
    sz := sz - rsz ;
    rsz := BlockRead( f, p^, sz ) ;
    client.Socket.SendBuf( p^, rsz ) ;
  end ;
  FreeMem( p, sizeof( byte ) * osz ) ;
  CloseFile( f ) ;
end ;


and in the OnClientRead event of the server socket:

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  f : file ;
  p : ^byte ;
  rsz, sz : longint ;
begin
  AssignFile( f, 'FileFromSocket' + Inttostr( Socket.Handle ) ) ;
  {$i-}Reset( f, 1 ) ;{$i+}
  sz := socket.ReceiveLength ;
  p := allocmem( sizeof( byte ) * sz ) ;
  rsz := Socket.RecieveBuf( p^, sz ) ;
  ReallocMem( p, sizeof( byte ) * rsz ) ;
  BlockWrite( f, p^, rsz ) ;
  CloseFile( f ) ;
  Freemem( p, sizeof( byte ) * rsz );
end;

If you want to send the filename as well (which I guess that you do), then just add a 256byte buffer onto the start of your pointer to byte, so that this can store the buffered filename...this will allow you 256 character filenames, but you will just have to write the code to memcopy the first 256 bytes out of the incoming buffer, convert it to a string, trim it, and use it as the filename...

Should do it...

Good luck...

Tim.

PS:  Ask again if this doesnt work, and I will write that bit for you too (for a few more crdits ;) heh heh heh ) -- or just mail me... ;O)


0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question