I would like to know how to trigger an event only when the IdTCPClient
component receives some data...
Like the native Delphi TCPClient component...
Thank you for your answers...
Uros Gaber
PowerCom Gaber & Globocnik d.n.o.
eMail: ur...@powercom-si.com
>Hi!
>
>I would like to know how to trigger an event only when the IdTCPClient
>component receives some data...
>Like the native Delphi TCPClient component...
You're focusing it in the wrong way. Indy is synchronous (blocking
sockets). You connect, read/write and disconnect. It is not based on
events.
What do you need to do? Maybe you could do it in some other way.
I wrote a client/server app. the server part is on Linux and the client part
written in Delphi. I used the native Delphi TCPClient component, and it
worked great... Now I wanted to implement SSL for connection between this
two parties...
The connection is established and also I can send data to server, but
whenever the server returns something I don't see it...
Thanks!
Uros Gaber
PowerCom Gaber & Globocnik d.n.o.
eMail: ur...@powercom-si.com
"Hadi Hariri - Team Indy" <had...@pbe.com> wrote in message
news:3ad5934e_2@dnews...
procedure TForm1.IdTCPClient1Work(Sender: TObject; AWorkMode: TWorkMode;
const AWorkCount: Integer);
begin
case AWorkMode of
wmRead: caption:= 'Reading from socket...';
wmWrite: caption:= 'Writing to socket...';
end;
end;
unit receivethread;
interface
uses Classes;
type
Treceivethread = class(TThread)
private
{ Private declarations }
IncomingData:string;
protected
procedure Execute; override;
procedure readevent;
end;
implementation
{ Treceivethread }
uses
mainformU;
procedure Treceivethread.Execute;
begin
try
while (not Terminated) AND (mainform.myClient.Connected) do begin
if mainform.myClient.Connected then begin
IncomingData := mainform.myClient.ReadLn('', 5000);
if (not mainform.myClient.ReadLnTimedOut)
and (IncomingData <> '') then Synchronize(readevent);
end else Terminate;
end;
except
Terminate;
end;
end;
procedure Treceivethread.readevent;
begin
mainform.IdTCPClientread(IncomingData);
end;
end.
You could use the Intercept property along with a TidLogDebug
or try this simple component i made. It has events for client reading data
or client writing data to the socket. It implements itself the TidLogDebug.
{
Indy IdTCPClientEx - Version 0.1
Original Programmer: Jan Pedersens (E-Mail: j...@jpsoft.dk
HTTP://www.jpsoft.dk)
No Copyright. Code is given to the Indy Pit Crew.
Quick Notes:
This is still rough.
If you find any problems, let me know at j...@jpsoft.dk.
What the component does:
The component notifies you on data being read or written from/to
the client. As the terminology of Indy is blocking sockets the
component does not violate this and it should be kept this way.
}
unit idTCPClientEx;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, idLogDebug;
type
TClientReadEvt = procedure(ASender: TComponent; const AText: string) of
object;
TClientWriteEvt = procedure(ASender: TComponent; const AText: string) of
object;
TidTCPClientEx = class(TidTCPClient)
protected
FInterceptLog: TidLogDebug;
FOnClientRead: TClientReadEvt;
FOnClientWrite: TClientWriteEvt;
procedure DoOnClientData(ASender: TComponent; var AText: string);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnClientRead: TClientReadEvt read FOnClientRead write
FOnClientRead;
property OnClientWrite: TClientWriteEvt read FOnClientWrite write
FOnClientWrite;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Indy Clients', [TidTCPClientEx]);
end;
constructor TidTCPClientEx.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FInterceptLog := TidLogDebug.Create(nil);
FInterceptLog.Target := ltDebugOutput;
FInterceptLog.LogTime := FALSE;
FInterceptLog.Active := TRUE;
FInterceptLog.OnLogItem:= DoOnClientData;//FOnClientData;
Intercept := FInterceptLog;
InterceptEnabled := TRUE;
end;
destructor TidTCPClientEx.Destroy;
begin
if Assigned(FInterceptLog) then
FreeAndNil(FInterceptLog);
inherited Destroy;
end;
procedure TidTCPClientEx.DoOnClientData(ASender: TComponent; var AText:
string);
var
recv: string;
p: integer;
begin
if pos('RECV:', UpperCase(AText)) = 1 then
begin
p:= pos('<EOL>', AText);
recv:= copy(AText, 7, p-7);
if Assigned(FOnClientRead) then
FOnClientRead(ASender, recv);
end else
if pos('SENT:', UpperCase(AText)) = 1 then
begin
p:= pos('<EOL>', AText);
recv:= copy(AText, 7, p-7);
if Assigned(FOnClientWrite) then
FOnClientWrite(ASender, recv);
end;
end;
end.
/Jan
I've implemented your unit... It works - to some point... It does read data
received from server, but I also use SSL (TIdConnectionInterceptOpenSSL) and
when I want to disconnect from the server I get access violation error... Is
this normal? Should I kill the thread before disconnect? Where do you think
is the problem?
Thank you!
Uros Gaber
PowerCom Gaber & Globocnik d.n.o.
eMail: ur...@powercom-si.com
"Tony" <tca...@execpc.com> wrote in message news:3ad5a5c5$1_2@dnews...
You could kill the thread when you disconnect also.
Tony
"Uros Gaber" <ur...@powercom-si.com> wrote in message
news:3ad5f812_2@dnews...
I do get the error in the IDE and not when I'm running the compiled exe...
Is it possible to "outcast" the exception in IDE?
Thanks for the note...
Uros Gaber
PowerCom Gaber & Globocnik d.n.o.
eMail: ur...@powercom-si.com
"Tony" <tca...@execpc.com> wrote in message news:3ad6352b$1_2@dnews...
You can turn off the exceptions with the compiler options I think, but then
you wont' see any.
Tony
"Uros Gaber" <ur...@powercom-si.com> wrote in message
news:3ad6aeee_1@dnews...