Indy; How do I know the size of the required buffer?

I have some problem when using the Indy component ( idTCPServer ) to read the data sending by the client, the data itself was hexadecimal, so I can not use AThread.Connection.ReadLn (); for this...

Here are my sample data sent by the client

24 24 00 11 12 34 56 FF FF FF FF 50 00 8B 9B 0D 0A

or

24 24 00 13 12 34 56 FF FF FF FF 90 02 00 0A 8F D4 0D 0A

PS : it is in hexadecimal bytes (data length may vary depending on the command, maximum 160 bytes ), which cannot get a string representation, since $ 00 is translated to null (which means that I can not use ReadLn)

Here is my sample code

procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
  Msg : Array[0..255] of Byte;
begin      
  AThread.connection.ReadBuffer(Msg,SizeOf(Msg));
  AThread.connection.WriteBuffer(Msg,MsgSize,true);
end;

, 255 , , ,

procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
  Msg : Array of Byte;
  MsgSize : integer;
begin  
  MsgSize := AThread.connection.ReadInteger; //doesn't actually get packet length?
  SetLength(Msg, MsgSize);    
  AThread.connection.ReadBuffer(Msg,MsgSize);
  AThread.connection.WriteBuffer(Msg,MsgSize,true);
end;

, ( )? - ?

+3
2

: . TCP - , . , ( ) ( ).
TCP, . " ", ; , .

, TCP .
TIdTCPConnection Read:
, N , ( , ).

+4

, , . try ReadInteger(), , , 1- 2- , . :

procedure TfrmMain.IdTCPServerExecute(AThread: TIdPeerThread);
var
  Unknown: Smallint; // maybe a msg type?
  DataSize: Smallint;
  Data: Array of Byte;
begin
  Unknown := AThread.Connection.ReadSmallInt;
  DataSize := AThread.Connection.ReadSmallInt - 4;
  SetLength(Data, DataSize);
  AThread.Connection.ReadBuffer(Data[0], DataSize);
  //...
end;
+3

Source: https://habr.com/ru/post/1796204/


All Articles