Solved

Sending EXE file using (Indy 10.5.8.0) IdTCPCLient/IdTCPServer

Posted on 2012-03-13
17
2,477 Views
Last Modified: 2012-03-19
Hi,

   Anyone give me a real example of how to send an EXE file (size is up to 30 MB) from IdTCPClient to IdTCPServer.  and how to make a real progress bar indicating file sending/receiving progress. on both Applications (server & client).

 I am using Indy 10.5.8.0 with Delphi XE2.

My Client/Server Applications works only on my LAN, no WAN.

Many Thanks in Advance!
0
Comment
Question by:Ahmad_Egypt
[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
  • 9
  • 5
  • 2
  • +1
17 Comments
 
LVL 100

Expert Comment

by:mlmcc
ID: 37714494
Why do you need to do this?

Will existing WIndows tools not allow you to copy a file from one machine to another?

mlmcc
0
 

Author Comment

by:Ahmad_Egypt
ID: 37714529
it is a big system, we need employees to request update for an existing system from in-house IT, IT ppl update the application every now and then , and they just send a message using admin tool to all clients (employees 50+) telling them that there is an update available.

and then clients just request the update for the application. and admin tool will send it automatically.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 37714722
There are tools available that will continuously or on a schedule update all PCs connected to a network with the latest versions.  I don't recall specific names but have you looked into using one of those?

mlmcc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Ahmad_Egypt
ID: 37714741
why to use ready tools while I am a developer and can do it by my self ?

is it that hard using Indy ?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37714799
Try it somehow like this...

Client Code:

procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
  fn: string;
begin
  if (opendialog1.Execute) then fn:=opendialog1.FileName;

  if FileExists(fn) then begin
    fs := TFileStream.Create(fn, fmOpenRead);

    IdTCPClient1.Connect;
    IdTCPClient1.IOHandler.Write(fs);
    IdTCPClient1.Disconnect;
  end;
end;

Open in new window



Server code:

procedure TForm1.IdTCPServer1Execute(TheContext: TIdContext);
var
  fs: TFileStream;
begin
  fs := TFileStream.Create('c:\temp\tempfile.$$$', fmCreate);

  TheContext.Connection.IOHandler.ReadStream(fs);
end;

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 37714815
Just to learn more about Delphi and Indy...

Exchanging Data over the Network using Delphi - Part 2
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37714837
This might help you implementing a progress bar...

How to implement a progress bar with TIdTCPClient?
0
 

Author Comment

by:Ahmad_Egypt
ID: 37714886
Thommy I tried your code, it creates the file with 1 KB, and nothing after that.

and what about the progress ?
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37714994
Just wrote my code "quick & dirty" without testing it.

Should only show you the principle of transfering data with Indy!

Have a look at Indy's ImageServer Demo and also following former EE post especially for implementing progress bar:
Progress indicator using Indy's TCP client/server
0
 

Author Comment

by:Ahmad_Egypt
ID: 37715079
Well, file transferred, same size... But...

as you know it is an EXE file, but it is corrupted, when i try to run it, it says that an application (client application) is using the file.. I close the client application and try to run the transferred file  , it is not a valid EXE file, even it is the same size.!!!
0
 
LVL 19

Expert Comment

by:Thommy
ID: 37715287
Can you post you client and server code...
0
 

Author Comment

by:Ahmad_Egypt
ID: 37716108
its worked after I updated the line on the Server Code:

   AContext.Connection.IOHandler.ReadStream(FileStream);

To This:

   AContext.Connection.IOHandler.ReadStream(FileStream, -1, True);


Anyway, below find the Code:


Client Code:

procedure TForm1.SendFileToClient(FName: string; IPAdd: string);
var
  FileStream : TFileStream;
begin
      FileStream := TFileStream.Create(FName, fmOpenRead);
      try
        UpdateClient.Host := IPAdd;
        UpdateClient.Port := 2007;
        UpdateClient.Connect;
        try
           try
              UpdateClient.IOHandler.Write(FileStream);
           finally
               UpdateClient.Disconnect;
           end;
        except
           on E: EIdException do
           begin
               //Network error occurred during communication
               ShowAMessage(2, 'Error Sending Update Files');
           end;
           on E: Exception do
           begin
               //Unknown error occurred during communication
               ShowAMessage(2, 'Error Sending Update Files');
           end;
        end;
      except
        on E: EIdException do
        begin
          //Network error occurred while trying to connect
          ShowAMessage(2, 'Error Connecting with Client');
        end;
        on E: Exception do
        begin
            //Unknown error occurred while trying to connect
           ShowAMessage(2, 'Error Connecting with Client');
        end;
      end;
end;

Open in new window



Server Code:
procedure TForm1.UpdateServerExecute(AContext: TIdContext);
var
  FileStream : TFileStream;
  FileLocation : String;
begin
   FileLocation := ExtractFileDir(ParamStr(0)) + '\Updates\' + 'AccInv.exe';
   ShowMessage(FileLocation);
   FileStream := TFileStream.Create(FileLocation, fmCreate);
   AContext.Connection.IOHandler.ReadStream(FileStream, -1, True);
   FreeAndNil(FileStream);
end;

Open in new window

0
 

Author Comment

by:Ahmad_Egypt
ID: 37716148
As you see my Source code;

  Naming may confuse you, the server will send the file to the client, but I use a client component on Server Application , and Server component on Client Application, they are only just for sending files.

Now if you can help me with progress bar thing on both Server & Client.

and if there is something like IE download dialog box, it will be great Idea shwoing the Transfer speed, and some information about transfer process.

Thanks in Advance :)
0
 

Author Comment

by:Ahmad_Egypt
ID: 37718640
another problem with this code,

   After Send Complete, if I Close Server Application, the transferred file on Client machine became 1 KB size. But if I close the client application before server application, size still the same and file transferred is ok.
0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 37737764
Ftp components may be better suited for sending files:
The protocol itself actually gives a good name: File Transfer Protocol

Small sample clientside:
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  CopyFile('c:\temp\afile.tmp');
end;

procedure TForm1.CopyFile(aFileName: string);
var Source: TFileStream;
begin
  Source := TFileStream.Create(aFileName, fmOpenRead);
  try
    IdFtp1.Put(Source, ExtractFileName(aFileName));
  finally
    Source.Free;
  end;
end;

procedure TForm1.IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Int64);
begin
  ProgressBar1.Position := AWorkCount;
end;

procedure TForm1.IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCountMax: Int64);
begin
  ProgressBar1.Position := 0;
  ProgressBar1.Max := AWorkCountMax;
end;

Open in new window

0
 

Author Comment

by:Ahmad_Egypt
ID: 37737777
sorry I never used any FTP component,

so what should I put on Server application, TIdFTP also , or TIdFTPServer? can you please explain further more ?
0
 

Author Comment

by:Ahmad_Egypt
ID: 37737933
Yes It is, FTP rocks, Thanks:)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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