Solved

Indy 10 TCP send/receive file

Posted on 2006-06-28
4
6,458 Views
Last Modified: 2012-06-27
I need to know how to use Indy 10 to send and receive a file with TCP. I have some old examples from Indy 9, but I cant get them to work in Delphi 2006 and Indy 10.

Tomas
0
Comment
Question by:TomasThil
  • 3
4 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
I have an example on sourceforge for this
https://sourceforge.net/projects/internetdemos/

but basically, to make the indy 9 demo work for indy 10, you need to change just a couple of things
this is from memory, so excuse typos.

uses IDStreamVCL;

var
     AStream: TIdStreamVCL;

begin
      AStream := TIDStreamVCL.Create(
        TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'In\' + Filename, fmCreate), True);
     try
         AContext.Connection.IOHandler.ReadStream(AStream, Filesize, False);
     finally
         FreeAndNil(AStream);
     end;
end;


var
     AStream: TIdStreamVCL;

begin
      AStream := TIDStreamVCL.Create(
        TFileStream.Create(ExtractFilePath(Paramstr(0)) + 'In\' + Filename, fmOpenRead), True);
     try
         AContext.Connection.IOHandler.WriteStream(AStream, Filesize, False);
     finally
         FreeAndNil(AStream);
     end;
end;

-------------
Please note
-------------
If you are using later versions of Indy 10 than what ships, TIdStreamVSL was replaced with just TIDStream

uses IDStream, IdSyncObjs (might be IdSync, can't check right now)
and it's something like :-
var
   AStream: TIdStream
begin
  AStream := TIdFileStreamCreate(....


Let me know if you're using a later version and I'll look up the commands at work later on
0
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
What I tend to do is send the filename, the filesize, and then the file
That way, the server knows what to call the file, and how big it is supposed to be

for later versions of Indy 10 (which the Indy team recommend), you would do :-

--client

uses
        IdObjs, IdSys;

var
   AStream: TIDStream;

begin
   TCPClient1.Connect;
   TCPClient1.IOHandler.LargeStream := True; // fully support large streams


   AStream := TIdFileStream.Create(filename, fmOpenRead + fmShareDenyNone);
   try
       TCPClient1.IOHandler.Write('FILE'); //send command "FILE"
       TCPClient1.IOHandler.Write(ExtractFilename(filename)); // send file name
       TCPClient1.IOHandler.Write(IntToStr(AStream.Size)); //send file size

       TCPClient1.IOHandler.Write(AStream);
   finally
       FreeAndNil(AStream);
   end;
...



--server

uses
        IdObjs, IdSys;

procedure TfServerMain.IdTCPServer1Connect(AContext: TIdContext);
    begin
        AContext.Connection.IOHandler.LargeStream := True;
    end;


procedure TfServerMain.IdTCPServer1Execute(AContext: TIdContext);
    var
        InCmd: string;
        AStream: TIdStream;
        S: string;
        FileSize: Int64;
        Filename: string;
    begin
        InCmd := ReceiveStringWithLogging(AContext);
        if InCmd = 'FILE' then
        begin
//followed by separate FILENAME, then FILESIZE, then the actual STREAM
            Filename := ReceiveStringWithLogging(AContext);
            S := ReceiveStringWithLogging(AContext); // filesize
            FileSize := StrToInt(S);
            ForceDirectories(ExtractFilePath(Paramstr(0)) + 'In');
            AStream := TIDFileStream.Create(ExtractFilePath(Paramstr(0)) + 'In\' + Filename, fmCreate);
            try
                AContext.Connection.IOHandler.ReadStream(AStream, Filesize, False);
            finally
                FreeAndNil(AStream);
            end;
        end;
   end;
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 125 total points
Comment Utility
oh, umm, replace
Filename := ReceiveStringWithLogging(AContext);
S := ReceiveStringWithLogging(AContext); // filesize
with
Filename := AContext.Connection.IOHandler.ReadLn;
S := AContext.Connection.IOHandler.ReadLn; //filesize
0
 

Author Comment

by:TomasThil
Comment Utility
Thanks, been looking for a solution the last 4 days. Only one thing if some one else read this.
To get this to work the client code have to be changed from :

       TCPClient1.IOHandler.Write('FILE'); //send command "FILE"
       TCPClient1.IOHandler.Write(ExtractFilename(filename)); // send file name
       TCPClient1.IOHandler.Write(IntToStr(AStream.Size)); //send file size

To:

       TCPClient1.IOHandler.WriteLn('FILE'); //send command "FILE"
       TCPClient1.IOHandler.WriteLn(ExtractFilename(filename)); // send file name
       TCPClient1.IOHandler.WriteLn(IntToStr(AStream.Size)); //send file size

If not, the server get all text on one line.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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