TCP is a stream of bytes. You can send anything, but they all end up like a
stream of bytes at the other end. If you want to send and receive fixed size
messages, just make sure both sides know the length and always handle the
received chunks of that size like a message. If the size is variable, prefix a
small header giving info over the message, like size, type and so on.
Which datagram based protocols like UDP everything are packets and there's
a one to one relation between what you send and how the other will receive it.
The only difficulty is knowing how big the receive buffer should be, because if
it's too small you will lose data that didn't fit.
With UDP, if you send 100 2 byte packets, the receiver will need to call recv ()
100 times and get 2 bytes every time. With TCP, if the buffer is big enough, all
those 200 bytes may be received with one recv () call. Or more: You just don't
know and shouldn't care.
So you can't force the message size on either TCP or UDP. With TCP it's
all squashed together into a stream of bytes, and with UDP the size is as
big as the message you send, though the practical message size is limited
by the receiver's buffer size.
stream of bytes at the other end. If you want to send and receive fixed size
messages, just make sure both sides know the length and always handle the
received chunks of that size like a message. If the size is variable, prefix a
small header giving info over the message, like size, type and so on.
Which datagram based protocols like UDP everything are packets and there's
a one to one relation between what you send and how the other will receive it.
The only difficulty is knowing how big the receive buffer should be, because if
it's too small you will lose data that didn't fit.
With UDP, if you send 100 2 byte packets, the receiver will need to call recv ()
100 times and get 2 bytes every time. With TCP, if the buffer is big enough, all
those 200 bytes may be received with one recv () call. Or more: You just don't
know and shouldn't care.
So you can't force the message size on either TCP or UDP. With TCP it's
all squashed together into a stream of bytes, and with UDP the size is as
big as the message you send, though the practical message size is limited
by the receiver's buffer size.
http://www.developerweb.net/forum/showthread.php?t=5935&highlight=recv+length