<<Hello, I am a rookie faced with having to do a sockets implementation
for a project and I have come across a couple of varying philosophies on
how to implement the message exchange protocol for this. My needs are to
be able to transfer ASCII data messages (no binary data required). The
messages have a known terminating character (x07). I am soliciting some
opinions on the correct/feasible approach: Thank you.
Philosophy 1
The sender is obligated to send a message length (ala a short integer)
followed by the message content. The receiver will then use the given
length to continue a receive loop until the entire message has been
received.
Philosophy 2
The sender sends out a stream of data with a known terminating character
and the receiver must continue to receive data until the terminator has
been received.>>
I'm a fan of approach #1:
1) Sending a fixed-format header containing the length of the
accompanying data allows the receiver to properly allocate buffer space
for the incoming message.
2) It also allows the receiver to perform at least some integrity
checking on the received message; if the character at the end of the
message (as advertised in the header) is not the terminator, something
went wrong.
3) Open-ended input streams are frequent causes of a variety of problems.
That's why most programmers avoid scanf(); arbitrary unchecked input can
easily end up trashing the stack.
Steve