1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| unit ClientSocketClasses;
interface
uses Windows, Messages, Classes, SysUtils, ScktComp;
type
TClientApplication = class(TObject)
private
FSocket: TClientSocket;
FStream: TStream;
procedure OnConnectEventHandler(Sender: TObject; Socket: TCustomWinSocket);
public
destructor Destroy(); override;
procedure Initialize();
procedure Run();
end;
function Application(): TClientApplication;
implementation
{ TClientApplication }
{ TClientApplication - Singleton }
var
_Application: TClientApplication = nil;
function Application(): TClientApplication;
begin
if not Assigned(_Application) then
_Application := TClientApplication.Create();
Result := _Application;
end;
{ TClientApplication - Constructeurs }
destructor TClientApplication.Destroy();
begin
if Assigned(FStream) then
begin
FStream.Free();
FStream := nil;
end;
if Assigned(FSocket) then
begin
FSocket.Free();
FSocket := nil;
end;
inherited Destroy();
end;
{ TClientApplication - Méthodes Publiques }
procedure TClientApplication.Initialize;
begin
FSocket := TClientSocket.Create(nil);
FSocket.Port := 6667;
FSocket.Host := '127.0.0.1';
FSocket.ClientType := ctBlocking;
FSocket.OnConnect := OnConnectEventHandler;
FSocket.Open();
end;
procedure TClientApplication.Run;
var
Msg: TMsg;
Terminated: Boolean;
ReadText: string;
WriteText: string;
begin
Terminated := False;
SetLength(ReadText, 1024);
repeat
WriteText := FormatDateTime('YYYY-MM-DD HH:NN:SS:ZZZ', Now());
FStream.Write(WriteText[1], Length(WriteText));
if FStream.Read(ReadText[1], Length(ReadText)) > 0 then
Terminated := Pos('QUIT', ReadText) > 0;
until Terminated;
end;
{ TClientApplication - Méthodes Privées }
procedure TClientApplication.OnConnectEventHandler(Sender: TObject; Socket: TCustomWinSocket);
begin
FStream := TWinSocketStream.Create(Socket, 1000);
end;
initialization
finalization
if Assigned(_Application) then
begin
_Application.Free();
_Application := nil;
end;
end. |
Partager