Re: Correct way to read records in Indy 10 with TIdTCPClient

Giganews Newsgroups
Subject: Re: Correct way to read records in Indy 10 with TIdTCPClient
Posted by:  Remy Lebeau (Indy Team) (no.spam@no.spam.com)
Date: Mon, 21 Aug 2006

"Grant Brown" <gra…@sitedoc.com.au> wrote in message
news:A4236B98C804E340gra…@sitedoc.com.au...

> Is the following the correct manner in which to read records
> using the TIdTCPClient component in Indy 10.

No.  You are calling ReadBytes(), which fills a TIdBytes.  But you are
specifying SizeOf(ByteBuf) where you need to use SizeOf(NewLicRec)) instead.
You can't call SizeOf() on a TIdBytes, it will alyways report the wrong
value.

> Also how would I gard against a lost connection during the repeat loop

The reading will throw an exception that you are not catching yet, so the
loop will automatically exit when the connection is lost.

Try this code instead:

    SetLength(ByteBuf, 0);
    with IdTCPClient do
    try
        repeat
            if SendCmd(CmdStr_NewLic, [203, 204]) = 204 then
            begin
                IOHandler.ReadBytes(ByteBuf, SizeOf(NewLicRec));
                BytesToRaw(ByteBuf, NewLicRec, SizeOf(NewLicRec));
                Break;
            end else begin
                IOHandler.InputBuffer.RemoveBytes(SizeOf(NewLicRec));
            end;
        until False;
    except
        on E: Exception do ShowMessage(E.Message);
    end;

Gambit

Replies

None

In response to

Correct way to read records in Indy 10 with TIdTCPClient posted by Grant Brown on Mon, 21 Aug 2006