Quote:So I should add a header to the message letting the server know how long the message is?
yes, you should have some kind of way to detect if a message the sender sends have been fully received by the receiver. A header preceding the size of the message is a good idea. There there recipient can tell how big the 'payload' of the message is.
Quote:and only send the message when all of the data has been recieved?
not necessary. You can just pump messages out if the sender does not need to know when the recipient processes the message. send message A, send message B, send message C... The recipient will be responsible for re-constructing the messages and waiting to receive the full payloads *[1].
if you need some sort of synchronisation (i.e. the server should wait until the client, or all clients received the message before doing something else), you'll need some form of aknowledgement system, where the recipient sends back a reply, saying ('I've received message A', 'I've received message B', ....).
if you prefix your messages with a sequence number in the header (an number that increases by one for each message the server sends), the recipient can just send back the sequence number that identifies the message uniquely.
[1] It's best if you have some sort of caching mechanism, since it is quite possible your recipients will even receive incomplete message headers, let alone incomplete payloads, until the remainder is received through the socket later on.
Same thing should be done if you need your sender to process aknowledgements, since an ack itself (say a 4 byte number) may be also fragmented (i.e. server receive the first three bytes of the ack in one recv(), then the last byte in another recv()).
if you want a 'what packet I send is what packet you receive' system, UDP will do that. However, the datagrams you send through UDP can be.
1) not received at all.
2) out of order.
3) duplicated.
[Edited by - oliii on December 18, 2008 12:57:10 PM]