Solved

Send/Receive file with TClient/TServerSocket

Posted on 1998-09-12
1
341 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

679 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