Solved

Indy Trivial FTP Client/Server Example

Posted on 2004-09-13
5
4,624 Views
Last Modified: 2012-08-14
I'm looking for an example on how to use the Indy Trivial FTP client and server components to upload and download a file.

Get and Put with the client seems clear enough, but I'm unsure how to handle the requests in te server.

Thanks in advance,

D2.
0
Comment
Question by:wavget
  • 2
  • 2
5 Comments
 
LVL 8

Expert Comment

by:BdLm
ID: 12046177
did you check the samples from the indy homepage ?
0
 
LVL 8

Expert Comment

by:BdLm
ID: 12046247
0
 
LVL 1

Author Comment

by:wavget
ID: 12053740
First of all, Delphi comes with an example of both a TrivialFtp Server and Client, I just didn't think to look there at first :)

I downloaded the demo from indyproject.org but I am unable to find a TrivialFtp example in it.

My biggest question is how do I handle OnRead and OnWrite in the server.
0
 
LVL 1

Accepted Solution

by:
NickRyan earned 100 total points
ID: 12063094
Here's a short sample of how you could handle the file read process. The file write process is pretty similar except that you may want to put in more checks in the case of overwriting files.

This short example assumes that you're only accepting filenames, not filepaths in the TFTP server - if you want to accept file paths as well then you need to process the directory separators and ensure that it's not possible for a user to reference files outside of where you want them to. ie, strip out any "..\" items in the path.

You can use the IP address of the client as a form of access control if you need or want to restrict access to certain IP addresses or address ranges.


procedure TForm1.IdTrivialFTPServer1ReadFile( Sender:TObject;
  var FileName:String; const PeerInfo:TPeerInfo;
  var GrantAccess:Boolean; var AStream:TStream;
  var FreeStreamOnComplete:Boolean );
var
  EffectiveFileName:string;
const
  SERVER_ROOT = 'C:\TFTP\';
begin

  {First, check IP address of client, if this is what you need to do}
  // PeerInfo.PeerIP is the IP address of the client
  // if not PeerInfo.PeerIP = AllowedIPAddress then begin
  //   GrantAccess := False;
  //   Exit;
  // end; {if}

  {Process filename - in this case we assume that we are dealing with just a filename with no dir part}
  EffectiveFileName := SERVER_ROOT + ExtractFileName(FileName);
  GrantAccess := FileExists(EffectiveFileName);
  if not GrantAccess then begin
    {File doesn't exist, so obviously can't allow access}
    False;
    Exit;
  end; {if}

  {Setup file access}
  try
    {Open a file stream referencing the file}
    AStream := TFileStream.Create( EffectiveFileName, fmOpenRead or fmShareDenyWrite );
    {Allow the server FTP component to destroy the stream when the transfer process finishes}
    FreeStreamOnComplete := True;
  except
    {Handle failure to open file stream here - ie, file locked}
    GrantAccess := False;
  end; {try}

end;
0
 
LVL 1

Expert Comment

by:NickRyan
ID: 12063281
Oops - please excuse the extra "False;" line in just after the FileExists check... it's just a typo.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now