Note that there are some explanatory texts on larger screens.

plurals
  1. PODelphi: Indy TIdTCPClient Reading Data
    text
    copied!<p>I am using Delphi 2007 &amp; Indy 10; I am a bit of a Delphi noob so apologies if I have missed something obvious...</p> <p>Background: I have a simple server app which simply sends the word "PING" when you connect to its port. It will also respond if it receives the word "PONG". This is working fine, I have manually tested this using netcat/wireshark.</p> <p>I am trying to code my client to connect to the port and automatically respond to the word PING whenever it receives it. I have created a simple form with a button to manually connect.</p> <p>The client connects, but it does not respond to the word PING. I think the problem lies with:</p> <pre><code>TLog.AddMsg(FConn.IOHandler.ReadLn); </code></pre> <p>My debug log reports only as far as "DEBUG: TReadingThread.Execute - FConn.Connected".</p> <p>My client code:</p> <pre><code>unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdCustomTransparentProxy, IdSocks, IdBaseComponent, IdComponent, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdTCPConnection, IdTCPClient, IdSync; type TReadingThread = class(TThread) protected FConn: TIdTCPConnection; procedure Execute; override; public constructor Create(AConn: TIdTCPConnection); reintroduce; end; TLog = class(TIdSync) protected FMsg: String; procedure DoSynchronize; override; public constructor Create(const AMsg: String); class procedure AddMsg(const AMsg: String); end; TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; IdIOHandlerStack1: TIdIOHandlerStack; client: TIdTCPClient; IdSocksInfo1: TIdSocksInfo; procedure Button1Click(Sender: TObject); procedure clientConnected(Sender: TObject); procedure clientDisconnected(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; rt: TReadingThread = nil; implementation {$R *.dfm} constructor TReadingThread.Create(AConn: TIdTCPConnection); begin Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Create'); // Debug FConn := AConn; inherited Create(False); end; procedure TReadingThread.Execute; begin Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Execute'); // Debug while not Terminated and FConn.Connected do begin Form1.Memo1.Lines.Add('DEBUG: TReadingThread.Execute - FConn.Connected'); // Debug TLog.AddMsg(FConn.IOHandler.ReadLn); end; end; constructor TLog.Create(const AMsg: String); begin Form1.Memo1.Lines.Add('DEBUG: TLog.Create'); // Debug FMsg := AMsg; inherited Create; end; procedure TLog.DoSynchronize; var cmd : string; begin Form1.Memo1.Lines.Add('DEBUG: TLog.DoSynchronize'); // Debug cmd := copy(FMsg, 1, 1); if cmd='PING' then begin Form1.client.Socket.WriteLn('PONG'); end end; class procedure TLog.AddMsg(const AMsg: String); begin Form1.Memo1.Lines.Add('DEBUG: TLog.AddMsg'); // Debug with Create(AMsg) do try Synchronize; finally Free; end; end; procedure TForm1.FormCreate(Sender: TObject); begin Memo1.Clear; end; procedure TForm1.Button1Click(Sender: TObject); var Host : String; Port : Integer; begin Host := '127.0.0.1'; Port := StrToInt('1234'); client.Host := Host; client.Port := Port; with client do begin try Connect; except on E: Exception do Memo1.Lines.Add('Error: ' + E.Message); end; end; end; procedure TForm1.clientConnected(Sender: TObject); begin Form1.Memo1.Lines.Add('DEBUG: TForm1.clientConnected'); // Debug rt := TReadingThread.Create(client); end; procedure TForm1.clientDisconnected(Sender: TObject); begin Form1.Memo1.Lines.Add('DEBUG: TForm1.clientDisconnected'); // Debug if rt &lt;&gt; nil then begin rt.Terminate; rt.WaitFor; FreeAndNil(rt); end; end; end. </code></pre> <p>Any help/advice would be appreciated.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload