A TCP server is sending data frames continuosly every 8ms. I want to program a client able to receive these data frames. Is there any procedure in Indy 9 to know if there is data available in the buffer?
My current programs is the following (I am using a Thread):
procedure TThreadRead.Execute;
var
buffer: array [0..755] of byte;
//s1: string;
//i: integer;
begin
IdTCPClient1.RecvBufferSize:= 756;
IdTCPClient1.Connect;
while Terminated = false do
begin
if IdTCPClient1.InputBuffer.Size = 0 then
IdTCPClient1.ReadFromStack(True,0,False);
while IdTCPClient1.InputBuffer.Size > 0 do
begin
ReadBuffer(buffer, FClient.InputBuffer.Size);
//s1:= '';
//For i:=0 To Length(buffer)-1 Do
// s1:=s1+IntToHex(Ord(buffer[i]),2); //Read values-->global var
//Form1.Memo1.Text:=s1;
end;
end;
end;
Is there any more efficient solution for reading TCP data continuously (like onread event in UDP)?
Thanks in advance.
TIdTCPClient
is not an asynchronous component. It does not tell you when data arrives. You need to use a Timer or a Thread to periodically poll the socket for new data (TIdUDPServer
uses an internal thread to trigger itsOnUDPRead
event), eg:With that said,
CurrentReadBuffer()
is generally not the best choice to use. Typically you would do something more like this instead:Update: given new information about the frame structure and your switch to a thread, you should be doing this instead: