Solved

Send/Receive file with TClient/TServerSocket

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now