Solved

Indy 10 TCP send/receive file

Posted on 2006-06-28
4
6,686 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
[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
  • 3
4 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17004211
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
ID: 17005885
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
ID: 17005913
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
ID: 17007494
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi application Soap connection 5 152
Delphi Form ownership 4 158
delphi parse string to params 3 177
How to insert .png images to TClientDataSet .cds file? 2 84
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…

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