📄 ftpserv1.pas
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Description: This is a demo program showing how to use the TFtpServer
component to build a FTP server.
Waring: As this demo is writtent, full access is given to all
users to all files accessible by the computer running the demo.
In production program, you should add code to implement
security issues.
Creation: April 21, 1998
Version: 1.06
EMail: http://www.overbyte.be francois.piette@overbyte.be
http://www.rtfm.be/fpiette francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1998-2001 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be><francois.piette@overbyte.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
Oct 25, 2000 V1.02 Added "List Clients" menu item and coding.
Jun 18, 2001 V1.03 Display version informations
Jul 30, 2001 V1.04 Add Trim function for Delphi 1
Feb 26, 2002 V1.05 Add DisconectAll in main menu
Jun 07, 2002 V1.06 Added a processing thread (not for Delphi 1) for Get
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit FtpServ1;
interface
uses
WinTypes, WinProcs, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, IniFiles, FtpSrv, FtpSrvC, WSocket, StdCtrls, ExtCtrls, Menus,
Winsock;
const
FtpServVersion = 106;
CopyRight : String = ' FtpServer (c) 1998-2002 F. Piette V1.06 ';
WM_APPSTARTUP = WM_USER + 1;
type
TLogMsg = class(TComponent)
public
procedure Text(Prefix : Char; Msg : String);
end;
{$IFNDEF VER80} { Not for Delphi 1, sorry }
TGetProcessingThread = class; { Forward declaration }
{ We use our own client class to hold our thread }
TMyClient = class(TFtpCtrlSocket)
private
FWorkerThread : TGetProcessingThread;
end;
TGetProcessingThread = class(TThread)
protected
Server : TFtpServer;
Client : TMyClient;
public
procedure Execute; override;
end;
{$ENDIF}
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;
MnuListClients: TMenuItem;
N2: TMenuItem;
DisconnectAllMnu: 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);
procedure MnuListClientsClick(Sender: TObject);
procedure DisconnectAllMnuClick(Sender: TObject);
procedure FtpServer1GetProcessing(Sender: TObject;
Client: TFtpCtrlSocket; var DelayedSend : Boolean);
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;
{$IFNDEF VER80}
procedure WorkerThreadTerminated(Sender : TObject);
{$ENDIF}
end;
var
FtpServerForm: TFtpServerForm;
Log : TLogMsg;
implementation
{$R *.DFM}
const
MainTitle = 'FTP Server - http://www.overbyte.be';
{ 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;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFDEF VER80}
function TrimRight(Str : String) : String;
var
i : Integer;
begin
i := Length(Str);
while (i > 0) and (Str[i] in [' ', #9]) do
i := i - 1;
Result := Copy(Str, 1, i);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TrimLeft(Str : String) : String;
var
i : Integer;
begin
if Str[1] <> ' ' then
Result := Str
else begin
i := 1;
while (i <= Length(Str)) and (Str[i] = ' ') do
i := i + 1;
Result := Copy(Str, i, Length(Str) - i + 1);
end;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Trim(Str : String) : String;
begin
Result := TrimLeft(TrimRight(Str));
end;
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -