How to mix text and binary data over TClient/ServerSockets
Posted on 2004-07-30
Question previously asked in Win Networking forum. No response in two days.
I am programming in Delphi 6 using standard TClient/ServerSockets windows socket connections. I am writing both the Client and the Server programs, using non-blocking connections.
How do I mix normal text messages with binary (eg image) data?
The basic problem is that I don't know how long each message is.
For solid text, I can put an "End of Transaction" marker in between each message. No problem.
But for binary data, the data may naturally contain the EOT marker. Also the binary files may be "large" (over 64K bytes).
As I see it, the alternatives are:
1. The start of each message does contain a "type" flag, like "TXT" or "BIN", then...
2. Use a VERY unusual, multi-byte EOT marker, like hex "00FF00FF" or something. Not bullet-proof, but close.
3. Send the binary data as counted byte streams, that is: preceeded by the byte count. I hesitate to do that with "all" data, since most of the data is text and a lost byte is usually no big deal.
4. On the Server side, wait until the Client's receive buffer is empty before sending BIN data. Then wait again after sending. That way, for BIN data, the Client can always read until the buffer is empty.
The question is: What are other / better alternatives?