It is really difficult to find examples on how to build a FTP server using Indy, so
here you have the basics about it. This article is written supposing you use Indy
10.
First of all you should know that FTP works on commands. While with Indy you do not
need to implement these on your own, at least you should know that they exist and
which is their purpose. Some examples are:
RETR --> for retrieving files
PASV --> for server opening connection and sending data to client (active mode)
PORT --> for client opening connection and sending data to server(passive mode)
STOR --> for storing files
You have also to take in consideration that FTP works with 2 connections: one for
the commands and another for the data. You can find complete information about FTP
searching on Internet for RFC 959.
For your FTP server you will need a TIdFTPServer component, and I would also
suggest you add a TIdUserManager for the accounts. Not a requirement though,
because you can choose to have only anonymous users.
After adding the TidFTPServer the minima you need to do is following:
1 - Set the bindings, i.e., the IPs and ports to which your server will listen. You
can do this from the Object Inspector.
2 - Decide if your FTP server will always be listening. In this case set property
Active to true. Otherwise take in consideration you will need to activate it
somewhere if a client wants to connect.
3 - Implement the event handlers for OnRetrieveFile (RETR command) and OnStoreFile
(STOR). You will need to pass them a stream. For example:
1 2 procedure TDataModule.IdFTPServerRetrieveFile(ASender: TIdFTPServerContext;
3 const AFileName: string; var VStream: TStream);
4 begin5 VStream := TFileStream.Create(MyFileName, fmOpenRead or fmShareExclusive);
6 end;
7 8 procedure TDataModule.IdFTPServerStoreFile(ASender: TIdFTPServerContext;
9 const AFileName: string; AAppend: Boolean; var VStream: TStream);
10 begin11 VStream := TFileStream.create(MyFileName, fmCreate or fmShareExclusive ) ;
12 end;
Doing this you will have an extremely simple FTP Server. You can add more
functionality using the events provided by TidFTPServer. Have a look at them and
you will see that you can control changing directories, user logins, etc. You can
also add your own custom commands using the CommandHandlers property, but as long
as you are not 100% sure about what you are doing you should NOT implement the
protocol commands (like RETR and so on) on your own. TidFTPServer has command
handlers for all of them implemented.
You can find more information about TidFTPServer and other Indy 10 components at
http://docs.projectindy.org/online/frames.html?frmname=topic&frmfile=index.html
Hope this article can help some people, as I went almost crazy the first time I
searched info about FTP server demos ;-)