P
peta
Unregistered / Unconfirmed
GUEST, unregistred user!
我如果直接做成一般 exe tcpserver 就没问题, 但如果做成 service , 就连不上。找遍google找不到答案。[]...那位高手指点指点,有示范源码更好咯,200分感谢![8D]
================
dmDataBackup.dfm
================
object DataBackup: TDataBackup
OldCreateOrder = False
DisplayName = 'DataBackup'
Password = ''
ServiceStartName = '/administrator'
OnExecute = ServiceExecute
OnStart = ServiceStart
OnStop = ServiceStop
Left = 336
Top = 248
Height = 150
Width = 215
object SocketServer: TIdTCPServer
Bindings = <>
CommandHandlers = <>
DefaultPort = 23
Greeting.NumericCode = 0
MaxConnectionReply.NumericCode = 0
OnConnect = SocketServerConnect
OnExecute = SocketServerExecute
ReplyExceptionCode = 0
ReplyTexts = <>
ReplyUnknownCommand.NumericCode = 0
Left = 64
Top = 32
end
end
================
dmDataBackup.pas
================
unit dmDataBackup;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
IdBaseComponent, IdComponent, IdTCPServer;
type
TDataBackup = class(TService)
SocketServer: TIdTCPServer;
procedure SocketServerExecute(AThread: TIdPeerThread);
procedure ServiceExecute(Sender: TService);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure SocketServerConnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
DataBackup: TDataBackup;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
DataBackup.Controller(CtrlCode);
end;
function TDataBackup.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TDataBackup.SocketServerExecute(AThread: TIdPeerThread);
var
sCmd: string;
begin
with AThread.Connection do
begin
sCmd := ReadLn;
if SameText(Copy(sCmd, 0, 4), 'QUIT') then
begin
Disconnect();
end;
end;
end;
procedure TDataBackup.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
ServiceThread.ProcessRequests(false);
end;
end;
procedure TDataBackup.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
self.SocketServer.Active := false;
Stopped := true;
end;
procedure TDataBackup.ServiceStart(Sender: TService; var Started: Boolean);
begin
self.SocketServer.Active := true;
Started := true;
end;
procedure TDataBackup.SocketServerConnect(AThread: TIdPeerThread);
begin
AThread.Connection.WriteLn('connected...');
end;
end.
================
dmDataBackup.dfm
================
object DataBackup: TDataBackup
OldCreateOrder = False
DisplayName = 'DataBackup'
Password = ''
ServiceStartName = '/administrator'
OnExecute = ServiceExecute
OnStart = ServiceStart
OnStop = ServiceStop
Left = 336
Top = 248
Height = 150
Width = 215
object SocketServer: TIdTCPServer
Bindings = <>
CommandHandlers = <>
DefaultPort = 23
Greeting.NumericCode = 0
MaxConnectionReply.NumericCode = 0
OnConnect = SocketServerConnect
OnExecute = SocketServerExecute
ReplyExceptionCode = 0
ReplyTexts = <>
ReplyUnknownCommand.NumericCode = 0
Left = 64
Top = 32
end
end
================
dmDataBackup.pas
================
unit dmDataBackup;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
IdBaseComponent, IdComponent, IdTCPServer;
type
TDataBackup = class(TService)
SocketServer: TIdTCPServer;
procedure SocketServerExecute(AThread: TIdPeerThread);
procedure ServiceExecute(Sender: TService);
procedure ServiceStop(Sender: TService; var Stopped: Boolean);
procedure ServiceStart(Sender: TService; var Started: Boolean);
procedure SocketServerConnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
function GetServiceController: TServiceController; override;
{ Public declarations }
end;
var
DataBackup: TDataBackup;
implementation
{$R *.DFM}
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
DataBackup.Controller(CtrlCode);
end;
function TDataBackup.GetServiceController: TServiceController;
begin
Result := ServiceController;
end;
procedure TDataBackup.SocketServerExecute(AThread: TIdPeerThread);
var
sCmd: string;
begin
with AThread.Connection do
begin
sCmd := ReadLn;
if SameText(Copy(sCmd, 0, 4), 'QUIT') then
begin
Disconnect();
end;
end;
end;
procedure TDataBackup.ServiceExecute(Sender: TService);
begin
while not Terminated do
begin
ServiceThread.ProcessRequests(false);
end;
end;
procedure TDataBackup.ServiceStop(Sender: TService; var Stopped: Boolean);
begin
self.SocketServer.Active := false;
Stopped := true;
end;
procedure TDataBackup.ServiceStart(Sender: TService; var Started: Boolean);
begin
self.SocketServer.Active := true;
Started := true;
end;
procedure TDataBackup.SocketServerConnect(AThread: TIdPeerThread);
begin
AThread.Connection.WriteLn('connected...');
end;
end.