Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
492 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (1)


Complete TFTP Server example, using Indy componentsFormat this article printer-friendly!Bookmark function is only available for registered users!
Example of multi-thredded TFTP Server, using Indy components
Product:
Delphi 4.x (or higher)
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
08/04/2004
Search Keys:
delphi delphi3000 article borland vcl code-snippet multi-threadded TFTP server filetransfer Indy internet
Times Scored:
11
Visits:
7373
Uploader: Kim Sandell
Company: Celarius Oy
Reference: N/A
 
Question/Problem/Abstract:
There are not many good TFTP server examples out there, so I wrote this example of a multi-thredded TFTP Server, using Indy components.
Answer:



There are few good examples of TFTP servers, so I
wrote this complete server as an example.

If works like a Secure TFTP server, since it only
allows uploads/downloads from a specific directory.

The example assumes that you open a new project with
a new form (Form1), and drop one TFTP Server and TFTP
Client on the form, and two buttons.

The source below can be copied as such. Enjoy.

-----CUT-----CUT-----CUT-----CUT-----CUT-----CUT-----CUT-----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer,
  IdTrivialFTPServer, StdCtrls, IdUDPClient, IdTrivialFTP;

type
  TForm1 = class(TForm)
    IdTrivialFTPServer1: TIdTrivialFTPServer;
    IdTrivialFTP1: TIdTrivialFTP;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure IdTrivialFTPServer1ReadFile(Sender: TObject;
      var FileName: String; const PeerInfo: TPeerInfo;
      var GrantAccess: Boolean; var AStream: TStream;
      var FreeStreamOnComplete: Boolean);
    procedure IdTrivialFTPServer1TransferComplete(Sender: TObject;
      const Success: Boolean; const PeerInfo: TPeerInfo; AStream: TStream;
      const WriteOperation: Boolean);
    procedure IdTrivialFTPServer1WriteFile(Sender: TObject;
      var FileName: String; const PeerInfo: TPeerInfo;
      var GrantAccess: Boolean; var AStream: TStream;
      var FreeStreamOnComplete: Boolean);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    TFTPPath              : String;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
     IdTrivialFTPServer1.ThreadedEvent := True;
     IdTrivialFTPServer1.Active := True;
     { Set the path to where the files will be stored/retreived }
     TFTPPath := IncludeTrailingPathDelimiter('C:\Temp');
end;

procedure TForm1.IdTrivialFTPServer1ReadFile(Sender: TObject;
  var FileName: String; const PeerInfo: TPeerInfo;
  var GrantAccess: Boolean; var AStream: TStream;
  var FreeStreamOnComplete: Boolean);
Var
   FS : TFileStream;
begin
     FreeStreamOnComplete := True;
     Try
        { Convert UNIX style filenames to WINDOWS style }
        While Pos('/',Filename)<>0 do Filename[Pos('/',Filename)] := '\';
        { Assure that the filename DOES NOT CONTAIN any path information }
        Filename := ExtractFileName( Filename );
        { Check if file exists }
        If FileExists( TFTPPath+Filename ) then
        Begin
          { Open file in READ ONLY mode }
          FS := TFileStream.Create( TFTPPath+Filename,
                                    fmOpenRead OR fmShareDenyWrite );
          { Assign stream to variable }
          AStream := FS;
          { Set parameters }
          GrantAccess := True;
        End Else
        Begin
          GrantAccess := False;
        End;
     Except
        { On errors, deny access }
        GrantAccess := False;
        If Assigned(FS) then FreeAndNil( FS );
     End;
end;

procedure TForm1.IdTrivialFTPServer1WriteFile(Sender: TObject;
  var FileName: String; const PeerInfo: TPeerInfo;
  var GrantAccess: Boolean; var AStream: TStream;
  var FreeStreamOnComplete: Boolean);
Var
   FS : TFileStream;
begin
     Try
        { Convert UNIX style filenames to WINDOWS style }
        While Pos('/',Filename)<>0 do Filename[Pos('/',Filename)] := '\';
        { Assure that the filename DOES NOT CONTAIN any path information }
        Filename := ExtractFileName( Filename );
        { Open file in WRITE ONLY mode }
        FS := TFileStream.Create( TFTPPath+Filename,
                                  fmCreate OR fmShareExclusive );
        { Copy all the data }
        AStream := FS;
        { Set parameters }
        FreeStreamOnComplete := True;
        GrantAccess := True;
     Except
        { On errors, deny access }
        GrantAccess := False;
        If Assigned(FS) then FreeAndNil( FS );
     End;
end;

procedure TForm1.IdTrivialFTPServer1TransferComplete(Sender: TObject;
  const Success: Boolean; const PeerInfo: TPeerInfo; AStream: TStream;
  const WriteOperation: Boolean);
begin
     // Success = TRUE if the read/write operation was successfull
     // WriteOperation = TRUE if the client SENT a file to the server
     Try
        { Close the FileStream }
        If Assigned(AStream) then FreeAndNil(AStream);
     Except
     End;
end;

// Example of how to DOWNLOAD a file from the server
procedure TForm1.Button1Click(Sender: TObject);
Var
   ST : TMemoryStream;
begin
     ST := TMemoryStream.Create;
     IdTrivialFTP1.Get('testfile.dat',ST);
     If Assigned(ST) then
     begin
          ShowMessage('Filesize='+IntToStr(ST.Size));
          FreeAndNil(ST);
     end;
end;

// Example of how to UPLOAD a file to the server
procedure TForm1.Button2Click(Sender: TObject);
Var
   ST : TMemoryStream;
   I  : Integer;
   S  : String;
begin
     { Create stream }
     ST := TMemoryStream.Create;
     { Initialize data }
     S := 'This is a test file. It whould appear in the '+
          'TFTP Server''s upload directory.'+#13#10;
     { Store in stream }
     ST.Write( S[1], Length(S) );
     ST.Position := 0;
     { Send Stream to TFTP Server }
     IdTrivialFTP1.Put(ST,'textfile.txt');
     { Free Stream }
     If Assigned(ST) then FreeAndNil(ST);
     { Show a dialog }
     ShowMessage('Done!');
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
good
    bob ziz (Mar 20 2007 3:21PM)

iam try to code a udp file sender with splitter packet
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)