Solved

Indy 10 TCP send/receive file

Posted on 2006-06-28
4
6,559 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10, MySQL Query 4 143
Controlled Assessment GCSE - desperate help needed 4 84
How to load 2 images in same column in Delphi 2 45
RESTRequest Parameter 4 30
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

806 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