Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Indy: OnRead?!

0 views
Skip to first unread message

Uros Gaber

unread,
Apr 12, 2001, 7:05:45 AM4/12/01
to
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...

Thank you for your answers...

Uros Gaber
PowerCom Gaber & Globocnik d.n.o.
eMail: ur...@powercom-si.com


Hadi Hariri - Team Indy

unread,
Apr 12, 2001, 7:36:46 AM4/12/01
to
ur...@powercom-si.com (Uros Gaber) wrote in <3ad58c9c_1@dnews>:

>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.

Uros Gaber

unread,
Apr 12, 2001, 7:53:23 AM4/12/01
to
Hi!

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...

Jan

unread,
Apr 12, 2001, 9:06:39 AM4/12/01
to
Hi.
"Uros Gaber" <ur...@powercom-si.com> skrev i en meddelelse
news:3ad58c9c_1@dnews...
Have you tried using the OnWork Event.

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;

Tony

unread,
Apr 12, 2001, 8:58:16 AM4/12/01
to
I think you need to start a listener thread and when it receives data from a
readln you can syncronize the thread to a notify procedure in the main VCL
thread. Here is a thread unit that should get you started.

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.

Jan

unread,
Apr 12, 2001, 11:07:00 AM4/12/01
to
Hi.
"Jan" <jan@_jpsoft_.dk> skrev i en meddelelse news:3ad5a855$1_1@dnews...

> Hi.
> "Uros Gaber" <ur...@powercom-si.com> skrev i en meddelelse
> news:3ad58c9c_1@dnews...
> > 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...
> >
> > Thank you for your answers...
> >
> > Uros Gaber
> > PowerCom Gaber & Globocnik d.n.o.
> > eMail: ur...@powercom-si.com

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


Uros Gaber

unread,
Apr 12, 2001, 2:44:12 PM4/12/01
to
Hi!

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...

Tony

unread,
Apr 12, 2001, 7:10:07 PM4/12/01
to
Do you get the error in the IDE or when running the compiled exe?
I get a exception when running in the IDE, but the try except in the thread
should catch it when running outside of the exe.

You could kill the thread when you disconnect also.

Tony

"Uros Gaber" <ur...@powercom-si.com> wrote in message
news:3ad5f812_2@dnews...

Uros Gaber

unread,
Apr 13, 2001, 3:44:24 AM4/13/01
to
Hi!

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...

Tony Caduto

unread,
Apr 13, 2001, 10:06:13 AM4/13/01
to
Uros,
I forgot to mention that you need to add myclient.disconnect in your forms
close event and that will take care of the exception, you do not need to
kill the thread.
You will get a Indy exception, but it will not happen when running the exe.
Sorry I forgot that little piece of info.

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...

0 new messages