Lclient
Lclient
{$mode objfpc}{$H+}
uses
Classes, Crt, SysUtils, lNet;
type
{ TLTCPTest }
TLTCPTest = class
private
FQuit: boolean;
FCon: TLTcp; // the connection
{ these are all events which happen on our server connection. They are called
inside CallAction
OnEr gets fired when a network error occurs.
OnRe gets fired when any of the server sockets receives new data.
OnDs gets fired when any of the server sockets disconnects gracefully.
}
procedure OnDs(aSocket: TLSocket);
procedure OnRe(aSocket: TLSocket);
procedure OnEr(const msg: string; aSocket: TLSocket);
public
constructor Create;
destructor Destroy; override;
procedure Run;
end;
// implementation
constructor TLTCPTest.Create;
begin
FCon := TLTCP.Create(nil); // create new TCP connection with no parent component
FCon.OnError := @OnEr; // assign callbacks
FCon.OnReceive := @OnRe;
FCOn.OnDisconnect := @OnDs;
FCon.Timeout := 100; // responsive enough, but won't hog cpu
end;
destructor TLTCPTest.Destroy;
begin
FCon.Free; // free the connection
inherited Destroy;
end;
procedure TLTCPTest.Run;
var
s, Address: string; // message-to-send and address
c: char;
Port: Word;
begin
if ParamCount > 1 then begin // we need atleast one parameter
try
Address := ParamStr(1); // get address from argument
Port := Word(StrToInt(ParamStr(2))); // try to parse port from argument
except
on e: Exception do begin
Writeln(e.message); // write error on failure
Halt;
end;
end;
s := '';
var
TCP: TLTCPTest;
begin
TCP := TLTCPTest.Create;
TCP.Run;
TCP.Free;
end.