Reputation: 98
My client receives strings where the first 4 characters of the string define the length of the message.
Example string: 0034PDCS00001700kg00000000kg00001700kg
I'm finding that reading is blocked when using either TidTcpClient.ReadString method or ReadBytes method even though there's enough data.
I'm not sure what I am doing wrong. Would someone please give me advice?
FClient.IOHandler.ReadBytes(Buffer, 4, False);// this gets the length OK
len := BytesToString(Buffer).ToInteger;
FClient.IOHandler.ReadBytes(Buffer, len, True);// this blocks and doesn't move on
sReceive := BytesToString(Buffer);
Upvotes: 1
Views: 734
Reputation: 595349
What you have shown works perfectly fine for me when I test it. The length and the data are read correctly without blocking. So something else is going on, either the data being received is not what you have shown here, or the problem is happening in another piece of code that you have not shown.
In any case, you can simplify the code you have shown by using ReadString()
instead of ReadBytes()
to read both values:
len := FClient.IOHandler.ReadString(4).ToInteger;
sReceive := FClient.IOHandler.ReadString(len);
Upvotes: 1