Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Aug : Re: Read data with TIdTCPClient
| Subject: | Re: Read data with TIdTCPClient |
| Posted by: | "Remy Lebeau (TeamB)" (no.spam@no.spam.com) |
| Date: | Mon, 21 Aug 2006 12:55:09 |
"Grant Brown" <grant@sitedoc.com.au> wrote in message
news:44e98205@newsgroups.borland.com...
> 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