Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Send/Receive file with TClient/TServerSocket

Posted on 1998-09-12
1
Medium Priority
?
365 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 200 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

Technology Partners: 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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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 anti-spam), the admin…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

972 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