Recent

Author Topic: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.  (Read 6700 times)

Logic_Bomb

  • New Member
  • *
  • Posts: 41
[lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« on: August 27, 2011, 06:16:27 pm »
Heyy all,

I am trying to make my program auto-connect on form create & re-connect on disconnect... Trouble is, the lNet TCP component doesn't seem to know when it is connected or not... I have tried this on Formcreate:

Code: [Select]
     Repeat
             SafeSleep(1000);
             Memo1.Lines.Add('Attempting to Reconnect...');
             TCP.Connect('127.0.0.1',43590);
             Application.ProcessMessages;
             SafeSleep(2000);
     until TCP.Connected;

I have also tried this on FormCreate:

Code: [Select]
     While (not TCP.Connected) do begin
             SafeSleep(1000);
             Memo1.Lines.Add('Attempting to Reconnect...');
             TCP.Connect('127.0.0.1',43590);
             Application.ProcessMessages;
             SafeSleep(2000);
     end;

I have also tried using my own boolean and setting it to true upon TCPConnect, this also does not work.

In the TCPDisconnect procedure I have used near enough the same code, any ideas on how to get this working, also, I am running the first autoconnect procedure in the FormCreate section, unfortuantly this is causing the form not to show until the loop has finished, does anyone know how to get the form to load before starting the loop??

I am severely confused about this thus any help will be greatly appreciated.

Thanks

Logic~
« Last Edit: August 27, 2011, 08:35:59 pm by Logic_Bomb »

joseme

  • Full Member
  • ***
  • Posts: 128
    • Logosoft sistemas
Re: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« Reply #1 on: August 27, 2011, 08:24:10 pm »
If Client is the name of your TCP component, then you should test if Client.Connected instead of TCP.Connected.
Code: [Select]
     Repeat
             SafeSleep(1000);
             Memo1.Lines.Add('Attempting to Reconnect...');
             Client.Connect('127.0.0.1',43590);
             Application.ProcessMessages;
             SafeSleep(2000);
     until Client.Connected;
un aporte a la comunidad:
http://pascalylazarus.blogspot.com/

Logic_Bomb

  • New Member
  • *
  • Posts: 41
Re: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« Reply #2 on: August 27, 2011, 08:34:50 pm »
Sorry, I should have changed that, I changed the name of 'Client' in the repeat loop to TCP for the purpose of ease of understanding on the forums, the TCP connector is actually named 'Client' in my Application, I simply changed the name when posting to the forum so that people would know what it is.

I will edit my post accourdingly... Sorry about that mess-up & Thanks for the reply.

Any more help on this topic would be appreciated.

Thanks

Logic~

George Athanasiadis

  • New Member
  • *
  • Posts: 22
    • Computer Force
Re: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« Reply #3 on: August 27, 2011, 10:56:07 pm »
Try this

  TForm1 = class(TForm)
      ............
      private
         FNet         : TLConnection;
         FIsServer : Boolean;
         FSendIndex : Integer;
         FBuffer    : String[255];
         FError     : Boolean;
         FGS        : String;
         FCntR      : Integer;
         FConnected : Boolean;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FNet := LTCP;
  FIsServer := False;
  FError := False;
  FConnected := False;
  TryConnect;
end;     

procedure TForm1.TryConnect;
begin
   Repeat
      Sleep(1000);
      MemoText.Lines.Add('Attempting to Reconnect...');
      if FNet.Connect('127.0.0.1', 43590) then FConnected := True
      Application.ProcessMessages;
      Sleep(1000);
  until FConnected;
end; 

procedure TForm1.LTCPComponentError(const msg: string; aSocket: TLSocket);
begin
  MemoText.Append(AnsiToUtf8(msg));
  MemoText.SelStart := Length(MemoText.Lines.Text);
  if Length(msg) > 0 then  FError := True;
  if Not FNet.Connected then TryConnect;
end;

George Athanasiadis

  • New Member
  • *
  • Posts: 22
    • Computer Force
Re: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« Reply #4 on: August 27, 2011, 11:11:55 pm »
I forgot the disconnect event

procedure TForm1.LTcpComponentDisconnect(aSocket: TLSocket);
begin
  MemoText.Append('Connection lost');
  MemoText.SelStart := Length(MemoText.Lines.Text);
  if Not FNet.Connected then TryConnect;
end;

Shebuka

  • Sr. Member
  • ****
  • Posts: 427
Re: [lNet] TCP - Auto-Connecting & Auto-Reconnecting.
« Reply #5 on: August 29, 2011, 10:46:15 am »
try this
Code: [Select]
     Repeat
             Memo1.Lines.Add('Attempting to Reconnect...');
             TCP.Connect('127.0.0.1',43590);
             //Application.ProcessMessages;
             //SafeSleep(2000);
             repeat
                     TCP.CallAction;
             until TCP.Connected;
     until TCP.Connected;

 

TinyPortal © 2005-2018