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.
The Implementation of an FTP Server (Page 1 of 6 )
Delphi 6 or higher and Indy 10.1.5, the latest snapshot. I've only tested this application on Windows XP, so, although I do not think you will encounter any problems on most other platforms, please let me know if you do. I will try to make the application as platform agnostic as possible.
To create our FTP server, we are going to use the idftpserver component available in Delphi. This component implements all the commands needed to create a viable server application. As always, I am going to implement as many commands as possible in this application, but by no means all of them. To implement an FTP server application that meets the minimum requirements you need to:
Be able to show all files in a given directory.
Change and remove a directory.
Upload and download files.
Delete and view file details.
Authenticate a user.
There are many more commands, but you can just about get on with implementing the minimum. To see how many commands you can implement with Indy, drop an idftpserver component on a form and look at the object inspectors events tab. Indy has done most of the interpreting of the FTP protocol, and thus makes it easy to implement the commands.
Start Delphi and create a new application. On the form add the following components:
In edit1's text property add "myusername" and on edit2's text property add "mypassword." Then from the Indy servers tab drop the idFTPServer component, and from the Indy intercepts tab drop a TidServerInterceptlogfile component; rename it to logfile1. Now we need to connect this intercept to the idftpserver component. So click on the idftpserver component, go to its intercept property on the object inspector and click on the drop down arrow. It should contain the word "logfile1." Select it and we're done.
On button1 add the caption "Connect" and on button2 add the caption "Exit." Double click on button1 and add the following code:
procedure TForm1.Button1Click(Sender: TObject);
Then do the same for button2 but add the following code:
procedure TForm1.Button2Click(Sender: TObject);
Button1 does a couple of things. It activates the ftpserver application and also sets the default port, which is number 21. The port number is very important because it is where the client application is going to try to connect to the server. Button2 just closes down the application. Your form should now look something like this: