Program HiddenServer;
{$APPTYPE CONSOLE}
{$R *.res}
Uses Windows, SysUtils, Classes, Forms, Graphics, IdContext,
IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer;
Type THandler =
Class(TComponent)
Public
Constructor Create(AOwner: TComponent);
Destructor Destroy;
Override;
Procedure ServerConnect(Context: TIdContext);
Procedure ServerExecute(Context: TIdContext);
Procedure ServerDisconnect(Context: TIdContext);
Procedure ServerStatus(Sender: TObject;
Const Status: TIdStatus;
Const StatusText:
String);
...
End;
Constructor THandler.Create(AOwner: TComponent);
Begin
Inherited;
...
End;
Destructor THandler.Destroy;
Begin
...
Inherited;
End;
Procedure THandler.ServerConnect(Context: TIdContext);
Begin
WriteLn('
#Connect: ', Context.Binding.PeerIP, '
:', Context.Binding.PeerPort);
...
End;
Procedure THandler.ServerExecute(Context: TIdContext);
Begin
...
If (beenden)
Then Application.Terminate;
...
End;
Procedure THandler.ServerDisconnect(Context: TIdContext);
Begin
WriteLn('
#Disconnect: ', Context.Binding.PeerIP, '
:', Context.Binding.PeerPort);
...
End;
Procedure THandler.ServerStatus(Sender: TObject;
Const Status: TIdStatus;
Const StatusText:
String);
Const X:
Array[TIdStatus]
of String = ('
haResolving', '
hsConnecting', '
hsConnected',
'
hsDisconnecting', '
hsDisconnected', '
hsStatusText', '
ftpTransfer', '
ftpReady', '
ftpAborted');
Begin
WriteLn('
#State (', X[Status], '
): ', StatusText);
End;
Var Handler: THandler;
Server: TIdTCPServer;
Function HandlerRoutine(CtrlType: LongWord): LongBool;
StdCall;
Begin
FreeAndNil(Server);
FreeAndNil(Handler);
Result := False;
End;
Begin
SetConsoleCtrlHandler(@HandlerRoutine, True);
Try
Application.Initialize;
Handler := THandler.Create(Application);
Try
Server := TIdTCPServer.Create(Handler);
Server.OnConnect := Handler.ServerConnect;
Server.OnExecute := Handler.ServerExecute;
Server.OnDisconnect := Handler.ServerDisconnect;
Server.OnStatus := Handler.ServerStatus;
Server.DefaultPort := 50000;
Server.Active := True;
Try
Repeat
Application.ProcessMessages;
Sleep(10);
Until Application.Terminated;
Finally
FreeAndNil(Server);
End;
Finally
FreeAndNil(Handler);
End;
Except
On E:
Exception do WriteLn(E.ClassName, '
: ', E.
Message);
End;
End.