又一ftp服务器源码

 

源码:
unit ServiceMainU;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  IdBaseComponent, IdComponent, IdTCPServer, IdFTPServer, Registry, forms,
  IdUserAccounts, IdThreadMgr, IdThreadMgrPool, IdThreadMgrDefault, IdFTPList;

type
  TServMain = class(TService)
    IdFTPServ: TIdFTPServer;
    IdUserMan: TIdUserManager;
    procedure ServiceAfterInstall(Sender: TService);
    procedure ServiceAfterUninstall(Sender: TService);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceExecute(Sender: TService);
    procedure IdFTPServAfterUserLogin(ASender: TIdFTPServerThread);
    procedure IdFTPServException(AThread: TIdPeerThread;
      AException: Exception);
    procedure IdFTPServRetrieveFile(ASender: TIdFTPServerThread;
      const AFileName: String; var VStream: TStream);
    procedure IdFTPServChangeDirectory(ASender: TIdFTPServerThread;
      var VDirectory: String);
    procedure IdFTPServListDirectory(ASender: TIdFTPServerThread;
      const APath: String; ADirectoryListing: TIdFTPListItems);
    procedure IdFTPServBeforeCommandHandler(ASender: TIdTCPServer;
      const AData: String; AThread: TIdPeerThread);
    procedure IdFTPServGetCustomListFormat(ASender: TIdFTPServer;
      AItem: TIdFTPListItem; var VText: String);
    procedure IdFTPServMakeDirectory(ASender: TIdFTPServerThread;
      var VDirectory: String);
    procedure IdFTPServUserLogin(ASender: TIdFTPServerThread;
      const AUsername, APassword: String; var AAuthenticated: Boolean);
    procedure IdFTPServStoreFile(ASender: TIdFTPServerThread;
      const AFileName: String; AAppend: Boolean; var VStream: TStream);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  ServMain: TServMain;

const
  csRegEventLog = 'SYSTEM/CurrentControlSet/Services/Eventlog/Application';
  csServiceName = 'ServMain';

implementation

uses MainU;

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  ServMain.Controller(CtrlCode);
end;

function TServMain.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

Procedure RegEventLog( aRegister : Boolean );
Var
  reg : TRegistry;
Begin
  reg := TRegistry.Create;
  Try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    If (reg.OpenKey(csRegEventLog, false)) Then
    Begin
      If (aRegister) Then
      Begin
        If (reg.OpenKey(csServiceName, true)) Then
        Begin
          reg.WriteString('EventMessageFile', ParamStr(0));
          reg.WriteInteger('TypesSupported', EVENTLOG_ERROR_TYPE
                        OR EVENTLOG_WARNING_TYPE OR EVENTLOG_INFORMATION_TYPE);
        End;
      End
      Else
        reg.DeleteKey(csServiceName);
    End;
  Finally
    reg.Free;
  End;
End;

procedure TServMain.ServiceAfterInstall(Sender: TService);
begin
  RegEventLog(true);
end;

procedure TServMain.ServiceAfterUninstall(Sender: TService);
begin
  RegEventLog(false);
end;

procedure TServMain.ServiceStart(Sender: TService; var Started: Boolean);
begin
  IdFTPServ.Active := true;//服务启动
  Started := true;
end;

procedure TServMain.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
  IdFTPServ.Active := false;//服务停止
  Stopped := true;
end;

procedure TServMain.ServiceExecute(Sender: TService);
begin
  while not Terminated do//服务响应消息
    begin
      ServiceThread.ProcessRequests(False);
      sleep(100);
    end;
end;

procedure TServMain.IdFTPServAfterUserLogin(ASender: TIdFTPServerThread);
begin
end;

procedure TServMain.IdFTPServException(AThread: TIdPeerThread;
  AException: Exception);
begin
//出错处理,以解决
end;

procedure TServMain.IdFTPServRetrieveFile(ASender: TIdFTPServerThread;
  const AFileName: String; var VStream: TStream);
begin//文件下载,响应Get,以解决
  VStream := TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone);
end;

procedure TServMain.IdFTPServChangeDirectory(ASender: TIdFTPServerThread;
  var VDirectory: String);
begin//改变路径,以解决
  if VDirectory[1] = '/' then
    VDirectory := 'E:/ftproot' + VDirectory;
  if pos(VDirectory, 'E:/ftproot/') <> 0 then
    VDirectory := 'E:/ftproot';
end;

procedure TServMain.IdFTPServListDirectory(ASender: TIdFTPServerThread;
  const APath: String; ADirectoryListing: TIdFTPListItems);
begin
end;

procedure TServMain.IdFTPServBeforeCommandHandler(ASender: TIdTCPServer;
  const AData: String; AThread: TIdPeerThread);
begin

end;

procedure TServMain.IdFTPServGetCustomListFormat(ASender: TIdFTPServer;
  AItem: TIdFTPListItem; var VText: String);
begin

end;

procedure TServMain.IdFTPServMakeDirectory(ASender: TIdFTPServerThread;
  var VDirectory: String);
begin//创建新路经,以解决
  VDirectory := StringReplace(VDirectory, '/', '/', [rfReplaceAll, rfIgnoreCase]);
  ForceDirectories(VDirectory);
end;

procedure TServMain.IdFTPServUserLogin(ASender: TIdFTPServerThread;
  const AUsername, APassword: String; var AAuthenticated: Boolean);
begin
  AAuthenticated := true;//用户登录验证,已实现
end;

procedure TServMain.IdFTPServStoreFile(ASender: TIdFTPServerThread;
  const AFileName: String; AAppend: Boolean; var VStream: TStream);
var//上传文件,终于解决
  hd: integer;
begin
  hd := filecreate(AFileName);
  fileclose(hd);
  VStream := TFileStream.Create(AFileName, fmOpenWrite or fmShareDenyNone);
end;

end;

end.  

// FTPServer.h: interface for the CFTPServer class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_FTPSERVER_H__144E8B64_2004_4709_B55A_242FE5F07BD2__INCLUDED_) #define AFX_FTPSERVER_H__144E8B64_2004_4709_B55A_242FE5F07BD2__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "ConnectThread.h" #include "ListenSocket.h" #include "FTPEventSink.h" #include "UserManager.h" #include "SecurityManager.h" class CFTPServer : public CWnd { friend CConnectSocket; public: void SetGoodbyeMessage(LPCTSTR lpszText); void SetWelcomeMessage(LPCTSTR lpszText); void SetTimeout(int nValue); void SetPort(int nValue); void SetMaxUsers(int nValue); void SetStatisticsInterval(int nValue); BOOL IsActive(); void Stop(); BOOL Start(); CFTPServer(); virtual ~CFTPServer(); CUserManager m_UserManager; CSecurityManager m_SecurityManager; CCriticalSection m_CriticalSection; // list of thread pointers CTypedPtrList<CObList, CConnectThread*> m_ThreadList; int GetPort() { return m_nPort; }; int GetMaxUsers() { return m_nMaxUsers; } int GetTimeout() { return m_nTimeout; } int GetConnectionCount() { return m_nConnectionCount; } CString GetWelcomeMessage() { return m_strWelcomeMessage; }; CString GetGoodbyeMessage() { return m_strGoodbyeMessage; }; void AddTraceLine(int nType, LPCTSTR pstrFormat, ...); private: // socket member that listens for new connections CListenSocket m_ListenSocket; CFTPEventSink *m_pEventSink; int m_nPort; int m_nMaxUsers; CString m_strWelcomeMessage; CString m_strGoodbyeMessage; int m_nTimeout; BOOL m_bRunning; // statistics DWORD m_dwTotalReceivedBytes; DWORD m_dwTotalSentBytes; int m_nConnectionCount; int m_nTotalConnections; int m_nFilesDownloaded; int m_nFilesUploaded; int m_nFailedDownloads; int m_nFailedUploads; // Operations public: BOOL IsIPAddressAllowed(LPCTSTR lpszIPAddress); void SetSecurityMode(BOOL bBlockSpecific = TRUE); BOOL CheckMaxUsers(); void Initialize(CFTPEventSink *pEventSink); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CFTPServer) //}}AFX_VIRTUAL // Generated message map functions protected: int m_nSecurityMode; int m_nStatisticsInterval; //{{AFX_MSG(CFTPServer) afx_msg void OnTimer(UINT nIDEvent); //}}AFX_MSG LRESULT OnThreadClose(WPARAM wParam, LPARAM lParam); LRESULT OnThreadStart(WPARAM wParam, LPARAM); LRESULT OnThreadMessage(WPARAM wParam, LPARAM lParam); DECLARE_MESSAGE_MAP() }; #endif // !defined(AFX_FTPSERVER_H__144E8B64_2004_4709_B55A_242FE5F07BD2__INCLUDED_)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值