TCP/IP Message Framing and Parsing [modified]
-
I want to send varying length application specific messages between my client and server application. The problem is how should I determine the length of message when receiving it? I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better? May I know why and how do I prefix it? Any code examples would be really appreciated. I'm using Winsocks (version 2.2, if that helps) And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP? Thanks
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
modified on Wednesday, August 26, 2009 6:25 AM
-
I want to send varying length application specific messages between my client and server application. The problem is how should I determine the length of message when receiving it? I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better? May I know why and how do I prefix it? Any code examples would be really appreciated. I'm using Winsocks (version 2.2, if that helps) And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP? Thanks
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
modified on Wednesday, August 26, 2009 6:25 AM
Ahmed Manzoor wrote:
I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?
Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.
Ahmed Manzoor wrote:
And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?
No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Ahmed Manzoor wrote:
I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?
Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.
Ahmed Manzoor wrote:
And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?
No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I don't understand what you mean by no minimum? Can you please explain?
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
If you turn off Nagling, then you can send a single byte in a TCP/IP packet. You can't get smaller than that (except 0, which is nothing).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
I don't understand what you mean by no minimum? Can you please explain?
Top Web Hosting Providers[^] Do, or do not. There is no 'try'.
You can send one byte at a time. You do NOT need to turn off Nagle algorithm in order to be able to send single bytes. Beside using a length prefix or a terminator, there is also a third possibility. If you send only one message per TCP connection, then close the connection gracefully[^]. The receiving peer will know when it has received the complete message and can close the socket. However, if you want to send more then one message, the first two possibilities would be better.
My webchat in Europe :java: (in 4K)
-
Ahmed Manzoor wrote:
I thought of one way was that I should append a termination string in the end of the message. But others said that prefixing the message length is far more better?
Prefix the length. That gives you a definite message length, which lets you detect errors better. Say you had a termination string and the packet containing the termination string was lost. Your program would keep searching and either a) never see the terminator, so think the message never ended, or b) see the wrong terminator (maybe from the next message) and merge two messages into one.
Ahmed Manzoor wrote:
And, by the way, what is the minimum amount of bytes/bits that will successfully transfer in one shot using TCP/IP?
No minimum really - but you need to turn off the Nagle algorithm (see setsockopt and TCP_NODELAY[^]).
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p