?? ftpserv1.pas
字號:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Description: This is a demo program showing how to use the TFtpServer
component to build a FTP server.
Creation: April 21, 1998
Version: 1.01
EMail: francois.piette@pophost.eunet.be
francois.piette@rtfm.be http://www.rtfm.be/fpiette
Support: Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1996, 1997, 1998 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
History:
Apr 29, 1998 V0.90 Released for beta testing.
Apr 30, 1998 V0.91 Added an example of virtual file (see the code for
FtpServer1RetrSessionConnected.
May 01, 1998 V0.92 Adapted for Delphi 1.0
May 03, 1998 V0.93 Adapted for Delphi 2.0 and C++Builder
May 04, 1998 V0.94 Added tools menu.
Jul 09, 1998 V1.00 Adapted for Delphi 4, removed beta status.
Jul 21, 1998 V1.01 Show how to refuse a client in OnClientConnected
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit FtpServ1;
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, FtpSrv, FtpSrvC, WSocket, StdCtrls, ExtCtrls, Menus;
const
FtpServVersion = 101;
WM_APPSTARTUP = WM_USER + 1;
type
TLogMsg = class(TComponent)
public
procedure Text(Prefix : Char; Msg : String);
end;
TFtpServerForm = class(TForm)
FtpServer1: TFtpServer;
InfoMemo: TMemo;
Panel1: TPanel;
StartMinimizedCheckBox: TCheckBox;
MainMenu1: TMainMenu;
File1: TMenuItem;
MnuStartServer: TMenuItem;
MnuStopServer: TMenuItem;
MnuQuit: TMenuItem;
N1: TMenuItem;
About1: TMenuItem;
GreenImage: TImage;
ClientCountLabel: TLabel;
RedImage: TImage;
Tools1: TMenuItem;
Cleardisplay1: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FtpServer1ClientConnect(Sender: TObject;
Client: TFtpCtrlSocket; Error: Word);
procedure FtpServer1ClientDisconnect(Sender: TObject;
Client: TFtpCtrlSocket; Error: Word);
procedure FtpServer1Start(Sender: TObject);
procedure FtpServer1Stop(Sender: TObject);
procedure FtpServer1ClientCommand(Sender: TObject;
Client: TFtpCtrlSocket; var Keyword, Params, Answer: TFtpString);
procedure FtpServer1StorSessionConnected(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
procedure FtpServer1StorSessionClosed(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
procedure FtpServer1RetrDataSent(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
procedure FtpServer1RetrSessionConnected(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
procedure FtpServer1RetrSessionClosed(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FtpServer1AnswerToClient(Sender: TObject;
Client: TFtpCtrlSocket; var Answer: TFtpString);
procedure FtpServer1Authenticate(Sender: TObject;
Client: TFtpCtrlSocket; UserName, Password: TFtpString;
var Authenticated: Boolean);
procedure FtpServer1ChangeDirectory(Sender: TObject;
Client: TFtpCtrlSocket; Directory: TFtpString; var Allowed: Boolean);
procedure MnuQuitClick(Sender: TObject);
procedure MnuStopServerClick(Sender: TObject);
procedure MnuStartServerClick(Sender: TObject);
procedure ImagesDblClick(Sender: TObject);
procedure FtpServer1BuildDirectory(Sender: TObject;
Client: TFtpCtrlSocket; var Directory: TFtpString; Detailed: Boolean);
procedure FtpServer1AlterDirectory(Sender: TObject;
Client: TFtpCtrlSocket; var Directory: TFtpString; Detailed: Boolean);
procedure Cleardisplay1Click(Sender: TObject);
private
FInitialized : Boolean;
FIniFileName : String;
FPort : String;
FXTop : Integer;
FXLeft : Integer;
FXWidth : Integer;
FXHeight : Integer;
procedure WMAppStartup(var msg: TMessage); message WM_APPSTARTUP;
procedure LoadConfig;
procedure SaveConfig;
procedure StartServer;
procedure StopServer;
procedure UpdateClientCount;
end;
var
FtpServerForm: TFtpServerForm;
Log : TLogMsg;
implementation
{$R *.DFM}
const
MainTitle = 'FTP Server - http://www.rtfm.be/fpiette';
{ Ini file layout }
SectionData = 'Data';
KeyPort = 'Port';
SectionWindow = 'Window';
KeyTop = 'Top';
KeyLeft = 'Left';
KeyWidth = 'Width';
KeyHeight = 'Height';
KeyMinim = 'RunMinimized';
STATUS_GREEN = 0;
STATUS_YELLOW = 1;
STATUS_RED = 2;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TLogMsg.Text(Prefix : Char; Msg : String);
begin
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FormShow(Sender: TObject);
var
IniFile : TIniFile;
Minim : Integer;
begin
if not FInitialized then begin
FInitialized := TRUE;
Caption := 'Starting ' + MainTitle;
Left := -Width;
IniFile := TIniFile.Create(FIniFileName);
FXTop := IniFile.ReadInteger(SectionWindow, KeyTop, Top);
FXLeft := IniFile.ReadInteger(SectionWindow, KeyLeft, Left);
FXWidth := IniFile.ReadInteger(SectionWindow, KeyWidth, Width);
FXHeight := IniFile.ReadInteger(SectionWindow, KeyHeight, Height);
Minim := IniFile.ReadInteger(SectionWindow, KeyMinim, 0);
IniFile.Free;
LoadConfig;
SaveConfig; { Create the inifile keys if they don't exists }
{ Be sure to always have the window visible }
{ with a reasonable width and height }
if FXLeft < 0 then
FXLeft := 0;
if FXTop < 0 then
FXTop := 0;
if FXWidth < 310 then
FXWidth := 310;
if FXHeight <= 250 then
FXHeight := 250;
if (FXLeft + FXWidth) > Screen.Width then
FXLeft := Screen.Width - FXWidth;
if (FXTop + FXHeight) > Screen.Height then
FXTop := Screen.Height - FXHeight;
StartMinimizedCheckBox.Checked := (Minim <> 0);
{ We use a custom message to initialize things once the form }
{ is visible }
PostMessage(Handle, WM_APPSTARTUP, 0, 0);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FormClose(Sender: TObject;
var Action: TCloseAction);
var
IniFile : TIniFile;
Minim : Integer;
begin
try
StopServer;
Minim := ord(StartMinimizedCheckBox.Checked);
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteInteger(SectionWindow, KeyTop, Top);
IniFile.WriteInteger(SectionWindow, KeyLeft, Left);
IniFile.WriteInteger(SectionWindow, KeyWidth, Width);
IniFile.WriteInteger(SectionWindow, KeyHeight, Height);
IniFile.WriteInteger(SectionWindow, KeyMinim, Minim);
IniFile.WriteString(SectionData, KeyPort, FPort);
IniFile.Free;
except
{ Ignore any exception when we are closing }
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.LoadConfig;
var
IniFile : TIniFile;
begin
IniFile := TIniFile.Create(FIniFileName);
FPort := IniFile.ReadString(SectionData, KeyPort, 'ftp');
IniFile.Free;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.SaveConfig;
var
IniFile : TIniFile;
begin
IniFile := TIniFile.Create(FIniFileName);
IniFile.WriteString(SectionData, KeyPort, FPort);
IniFile.Free;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{ This message handler is triggered by the FormShow event. We comes here }
{ only when the form is visible on screen. }
procedure TFtpServerForm.WMAppStartup(var msg: TMessage);
var
PrvWnd : HWND;
Buf : String;
begin
if StartMinimizedCheckBox.Checked then
Application.Minimize;
Top := FXTop;
Left := FXLeft;
Width := FXWidth;
Height := FXHeight;
{ Prevent the server from running twice }
Buf := ClassName + #0;
PrvWnd := FindWindow(@Buf[1], MainTitle);
if PrvWnd <> 0 then begin
Log.Text('E', 'Server already running. Shutdown.');
Close;
Exit;
end;
Caption := MainTitle;
Update; { It's nice to have the form completely displayed }
StartServer;
UpdateClientCount;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFNDEF VER80 }
{ To debug event driven programs, it is often handy to just use writeln to }
{ write debug messages to the console. To get a console, just ask the }
{ linker to build a console mode application. Then you'll get the default }
{ console. The function below will make it the size you like... }
procedure BigConsole(nCols, nLines : Integer);
var
sc : TCoord;
N : DWord;
begin
if not IsConsole then
Exit;
sc.x := nCols;
sc.y := nLines;
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), sc);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_BLUE or BACKGROUND_GREEN or
BACKGROUND_RED or BACKGROUND_INTENSITY);
sc.x := 0;
sc.y := 0;
FillConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
BACKGROUND_BLUE or BACKGROUND_GREEN or
BACKGROUND_RED or BACKGROUND_INTENSITY,
nCols * nLines, sc, N);
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FormCreate(Sender: TObject);
begin
{ Build Ini file name }
FIniFileName := LowerCase(ExtractFileName(Application.ExeName));
FIniFileName := Copy(FIniFileName, 1, Length(FIniFileName) - 3) + 'ini';
{ Create the Log object }
Log := TLogMsg.Create(Self);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -