Home arrow Delphi-Kylix arrow Page 6 - The Implementation of an FTP Server
DELPHI-KYLIX

The Implementation of an FTP Server


In the previous article, we discussed RFC 959, which defines how an FTP server should work. We also walked through some of the commands that are used in this protocol. In this article, we will be creating an example FTP server in Delphi.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 18
February 05, 2007
TABLE OF CONTENTS:
  1. · The Implementation of an FTP Server
  2. · The Code
  3. · Deleting Files
  4. · Listing Files
  5. · Making and Removing Directories
  6. · Authenticating Users

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
The Implementation of an FTP Server - Authenticating Users
(Page 6 of 6 )

This event authenticates a user that is trying to use the FTP server. It is advisable to use a database to authenticate a particular user. In the code below, I used "myusername" and "mypassword" as the username and password:

procedure TForm1.IdFTPServer1UserLogin(ASender:TIdFTPServerContext; const AUsername, APassword: String; var AAuthenticated:Boolean); begin if (AUsername = edit1.Text) and (APassword = edit2.Text) then begin AAuthenticated:=True end else begin AAuthenticated := False; end; end;

procedure TForm1.IdFTPServer1StoreFile(ASender:TIdFTPServerContext; const AFileName: String; AAppend: Boolean; var VStream:TStream); begin if not Aappend then VStream := TFileStream.Create(AFileName,fmCreate) else       VStream := TFileStream.Create(AFileName,fmOpenWrite) end;

procedure TForm1.Button1Click(Sender: TObject); begin idftpserver1.DefaultPort:=strtoint(edit3.text); idftpserver1.Active:=true; showmessage('Connected'); end;

procedure TForm1.IdFTPServer1AfterUserLogin(ASender:TIdFTPServerContext); begin //set home dir here homedir:='c:'; end;

This event is optional. It enables you to show a message when a exception occurs:

procedure TForm1.IdFTPServer1Exception(AContext: TIdContext; AException: Exception); begin showmessage(AException.Message); end;

procedure TForm1.Button2Click(Sender: TObject); begin idftpserver1.Active:=false; close; end;

The server status event simply displays the status of the server when  there is a status change:

procedure TForm1.IdFTPServer1Status(ASender: TObject; const AStatus: TIdStatus; const AStatusText: String); begin memo1.lines.add(Astatustext); end;

procedure TForm1.IdFTPServer1Stat(ASender:TIdFTPServerContext; AStatusInfo: TStrings); var i:integer; begin for i:= 0 to astatusinfo.Count-1 do begin memo1.Lines.Add(astatusinfo.Strings[i]); end; end;

This event gets fired when the server writes data to a client. I've included it mainly for debugging purposes. It will record all communications between the server and a client:

procedure TForm1.IdFTPServer1Execute(AContext: TIdContext); begin logfile1.DoLogWriteString(AContext.Connection.IOHandler.AllData); end;

That is all there is to implementing a working FTP server. As you will no doubt notice, I have not implemented all of the commands, but the ones I implemented cover all that is required for an FTP server to function well.

Conclusion

The best way to test this application is with a graphical FTP client such as Dreamweaver instead of telnet. For some reason  the server application refuses to execute the LIST command when telnet is used. And as I've stated many times before, you cannot do anything with an FTP server that cannot show you the files in a given directory. I've notified the Indy developers about this problem, and hopefully they will work on it.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials