Solved

Indy 10 TCP send/receive file

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

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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 …

679 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