I am tasked with working on a legacy D6 application that uses TClientSocket to request data from a TCP/IP Server which returns XML for which, I am required to integrate with a new 3rd party server and I am having an intermittent problem receiving the full data sent in the Server's response. When this happens I get the XML string over multiple OnRead events despite that the XML is pretty small, about 1.76 KB.
The structure of the response is that the first four bytes return indicate:
Byte Position, type/purpose:
0 - 0x02 (STX)
1 - Length, LSB
2 - Length
3 - Length, MSB
Bytes 4+ are the xml payload
However, the programmer before me simply used Socket.ReceiveText because all of the response being received for existing logic was very small, well under 200 bytes in most cases...essentially success confirmation or error data.
Would someone mind terribly giving me an idea of how I can successfully consume the response taking into account that I'm getting it in chunks? I've not used TClientSocket/TServerSocket much even when I was using Delphi regularly and I cannot change to something I am more familiar with.
Thanks in advance.
In the
OnRead
event, read whatever bytes are currently available on the socket and append them to a buffer. Then you can loop through that buffer extracting only complete XML messages and process them as needed, leaving incomplete XML messages in the buffer so they can be completed in laterOnRead
events.For example: