Solved

Send/Receive file with TClient/TServerSocket

Posted on 1998-09-12
1
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 112
Delphi application Soap connection 5 152
Sending files from  idTcpServer Socket to idTcpClient 2 65
Firemonkey Listview item popup menu ? 1 73
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…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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