Solved

Sending EXE file using (Indy 10.5.8.0) IdTCPCLient/IdTCPServer

Posted on 2012-03-13
17
2,530 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 101

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 101

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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