Solved

Indy 10 TCP send/receive file

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

623 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