⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftpsrv1.cpp

📁 互联网套件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Author:       Fran鏾is PIETTE
Description:  This is a demo program showing how to use the TFtpServer
              component to build a FTP server.
Creation:     Dec 19, 1998 (Translated from Delphi version)
Version:      1.02
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, 1999 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:
Aug 15, 1999  V1.02 Adapted for BCB4 (Moved FIniFileName initialization from
              FormCreate to form constructor).

 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//---------------------------------------------------------------------------
#include <vcl.h>
#include <vcl\inifiles.hpp>
#pragma hdrstop

#define FtpServVersion = 102;
#include "FtpSrv1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FtpSrv"
#pragma resource "*.dfm"
TFtpServerForm *FtpServerForm;
TLogMsg        *Log;
#define MainTitle "FTP Server - http://www.rtfm.be/fpiette"
// Ini file layout
#define SectionData       "Data"
#define KeyPort           "Port"

#define SectionWindow     "Window"
#define KeyTop            "Top"
#define KeyLeft           "Left"
#define KeyWidth          "Width"
#define KeyHeight         "Height"
#define KeyMinim          "RunMinimized"

#define STATUS_GREEN      0
#define STATUS_YELLOW     1
#define STATUS_RED        2

//---------------------------------------------------------------------------
__fastcall TLogMsg::TLogMsg(TComponent* Owner)
    : TComponent(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TLogMsg::Text(char Prefix, AnsiString Msg)
{
}
//---------------------------------------------------------------------------
__fastcall TFtpServerForm::TFtpServerForm(TComponent* Owner)
    : TForm(Owner)
{
    // Build Ini file name
    FIniFileName = LowerCase(ExtractFileName(Application->ExeName));
    FIniFileName = FIniFileName.SubString(1, FIniFileName.Length() - 3) + "ini";
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::FormCreate(TObject *Sender)
{
    // Create the Log object
    Log = new TLogMsg(this);

    InfoMemo->Clear();
    GreenImage->Visible = FALSE;
    RedImage->Visible   = TRUE;
    RedImage->Top       = GreenImage->Top;
    RedImage->Left      = GreenImage->Left;
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::FormShow(TObject *Sender)
{
    TIniFile *IniFile;
    int      Minim;

    if (!FInitialized) {
        FInitialized        = TRUE;
        Caption             = "Starting " MainTitle;
        Left = -Width;

        IniFile  = new TIniFile(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)
            FXLeft = 0;
        if (FXTop < 0)
            FXTop = 0;
        if (FXWidth < 310)
            FXWidth = 310;
        if (FXHeight <= 250)
            FXHeight = 250;
        if ((FXLeft + FXWidth) > Screen->Width)
            FXLeft = Screen->Width - FXWidth;
        if ((FXTop + FXHeight) > Screen->Height)
            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);
    }
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::FormClose(TObject *Sender,
      TCloseAction &Action)
{
    TIniFile *IniFile;
    int      Minim;

    try {
        StopServer();
        Minim   = StartMinimizedCheckBox->Checked;
        IniFile = new TIniFile(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 (TRUE) {
        // Ignore any exception when we are closing
    }
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::LoadConfig(void)
{
    TIniFile *IniFile;

    IniFile = new TIniFile(FIniFileName);
    FPort   = IniFile->ReadString(SectionData, KeyPort, "ftp");
    IniFile->Free();
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::SaveConfig(void)
{
    TIniFile *IniFile;

    IniFile = new TIniFile(FIniFileName);
    IniFile->WriteString(SectionData, KeyPort, FPort);
    IniFile->Free();
}
//---------------------------------------------------------------------------
// This message handler is triggered by the FormShow event. We comes here    
// only when the form is visible on screen.
void __fastcall TFtpServerForm::WMAppStartup(TMessage &Msg)
{
    HWND       PrvWnd;
    AnsiString Buf;

    if (StartMinimizedCheckBox->Checked)
        Application->Minimize();
    Top    = FXTop;
    Left   = FXLeft;
    Width  = FXWidth;
    Height = FXHeight;

    // Prevent the server from running twice
    Buf = ClassName();
    PrvWnd = FindWindow(&Buf[1], MainTitle);
    if (PrvWnd) {
        Log->Text('E', "Server already running. Shutdown.");
        Close();
        return;
    }
    Caption = MainTitle;
    Update();               // It's nice to have the form completely displayed

    StartServer();
    UpdateClientCount();
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::StartServer(void)
{
    GreenImage->Visible = FALSE;
    RedImage->Visible   = TRUE;
    Update();

    FtpServer1->Start();
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::StopServer(void)
{
    FtpServer1->Stop();
    FtpServer1->DisconnectAll();
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::MnuQuitClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------
void __fastcall TFtpServerForm::MnuStopServerClick(TObject *Sender)
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -