Results 1 to 10 of 10

Thread: Question about lnet :)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Here's the code I'm using:
    {$IFDEF WINDOWS}
    {$R *.res}
    {$ENDIF}
    {$DEFINE STATIC}

    {$Mode Delphi}

    uses
    lnet, crt, classes;

    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
    message: string;
    received: TStringlist;
    constructor Create;
    destructor Destroy; override;
    end;

    procedure TLTCPTest.OnDs(aSocket: TLSocket);
    begin
    //
    end;

    procedure TLTCPTest.OnRe(aSocket: TLSocket);
    var
    s: string;
    begin
    if aSocket.GetMessage(s) > 0 then
    begin
    message:='Received message "'+s+'"';
    end;
    end;

    procedure TLTCPTest.OnEr(const msg: string; aSocket: TLSocket);
    begin
    //
    end;

    constructor TLTCPTest.Create;
    begin
    FCon := TLTCP.Create(nil);
    FCon.OnError := OnEr;
    FCon.OnReceive := OnRe;
    FCOn.OnDisconnect := OnDs;
    FCon.Timeout := 100;
    end;

    destructor TLTCPTest.Destroy;
    begin
    FCon.Free; // free the connection
    inherited Destroy;
    end;

    procedure netconnect(server, portz: string);
    var
    Address: string;
    Port: Word;
    attempt: integer;
    quitting: boolean;
    begin
    address:=server;
    port:=word(strtoint(portz));
    attempt:=0;
    quitting:=false;
    tcp.FCon.Connect(Address, Port);
    repeat
    tcp.FCon.CallAction;
    attempt:=attempt+1;
    if attempt=9001 then quitting:=true;
    until tcp.fcon.Connected or quitting;
    end;

    procedure netmessage(str: string);
    begin
    tcp.FCon.SendMessage(str+#13#10);
    end;

    Begin
    tcp:=tltcptest.create;
    netconnect('192.168.0.1', '1111');
    netmessage('hello!');
    And after netmessage('hello!'); I use some other code that uses messages received and other stuff.
    Last edited by NickS; 29-08-2011 at 02:38 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •