📄 ftpserv1.pas
字号:
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);
{$IFNDEF VER80}
{ BigConsole(80, 100); }
{$ENDIF}
InfoMemo.Clear;
GreenImage.Visible := FALSE;
RedImage.Visible := TRUE;
RedImage.Top := GreenImage.Top;
RedImage.Left := GreenImage.Left;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.StartServer;
var
wsi : TWSADATA;
begin
GreenImage.Visible := FALSE;
RedImage.Visible := TRUE;
Update;
{ Display version info for program and use components }
wsi := WinsockInfo;
InfoMemo.Lines.Add(Trim(CopyRight));
InfoMemo.Lines.Add('Using:');
InfoMemo.Lines.Add(' ' + WSocket.CopyRight);
InfoMemo.Lines.Add(' ' + FtpSrv.CopyRight);
InfoMemo.Lines.Add(' Winsock:');
InfoMemo.Lines.Add(' Version ' +
Format('%d.%d', [WinsockInfo.wHighVersion shr 8,
WinsockInfo.wHighVersion and 15]));
InfoMemo.Lines.Add(' ' + StrPas(@wsi.szDescription));
InfoMemo.Lines.Add(' ' + StrPas(@wsi.szSystemStatus));
{$IFNDEF VER100}
{ A bug in Delphi 3 makes lpVendorInfo invalid }
if wsi.lpVendorInfo <> nil then
InfoMemo.Lines.Add(' ' + StrPas(wsi.lpVendorInfo));
{$ENDIF}
{$IFNDEF VER80}
{ If not running 16 bits, we use our own client class }
FtpServer1.ClientClass := TMyClient;
{$ENDIF}
FtpServer1.Start;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.StopServer;
begin
FtpServer1.Stop;
FtpServer1.DisconnectAll;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.MnuQuitClick(Sender: TObject);
begin
Close;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.MnuStopServerClick(Sender: TObject);
begin
StopServer;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.MnuStartServerClick(Sender: TObject);
begin
StartServer;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.ImagesDblClick(Sender: TObject);
begin
if FtpServer1.Active then
StopServer
else
StartServer;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.UpdateClientCount;
begin
if FtpServer1.ClientCount = 0 then
ClientCountLabel.Caption := 'No user'
else
ClientCountLabel.Caption := IntToStr(FtpServer1.ClientCount) + ' users';
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1ClientConnect(Sender: TObject;
Client: TFtpCtrlSocket; Error: Word);
begin
{ The next test shows how to refuse a client }
if Client.GetPeerAddr = '193.121.12.25' then begin
Client.SendStr('421 Connection not allowed.' + #13#10);
Client.Close;
Exit;
end;
InfoMemo.Lines.Add('! ' + Client.GetPeerAddr + ' connected');
UpdateClientCount;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1ClientDisconnect(Sender: TObject;
Client: TFtpCtrlSocket; Error: Word);
begin
InfoMemo.Lines.Add('! ' + Client.GetPeerAddr + ' disconnected');
UpdateClientCount;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1Start(Sender: TObject);
begin
GreenImage.Visible := TRUE;
RedImage.Visible := FALSE;
InfoMemo.Lines.Add('! Server started');
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1Stop(Sender: TObject);
begin
GreenImage.Visible := FALSE;
RedImage.Visible := TRUE;
InfoMemo.Lines.Add('! Server stopped');
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1StorSessionConnected(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
begin
if Error <> 0 then
InfoMemo.Lines.Add('! ' + Client.GetPeerAddr +
' Data session failed to open. Error #' +
IntToStr(Error));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TFtpServerForm.FtpServer1StorSessionClosed(Sender: TObject;
Client: TFtpCtrlSocket; Data: TWSocket; Error: Word);
begin
if Error <> 0 then
InfoMemo.Lines.Add('! ' + Client.GetPeerAddr +
' Data session closed. Error #' + IntToStr(Error));
end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -