[lNet] TCP - Auto-Connecting & Auto-Reconnecting. lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Logic_Bomb · 2011-08-27 16:16 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~ ---------------------------------------------------------------------- joseme · 2011-08-27 18:24 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; ---------------------------------------------------------------------- Logic_Bomb · 2011-08-27 18:34 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 · 2011-08-27 20:56 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 · 2011-08-27 21:11 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 · 2011-08-29 08:46 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;