views:

71

answers:

3

Our server is seemingly packet based. It is an adaptation from an old serial based system. It has been added, modified, re-built, etc over the years. Since TCP is a stream protocol and not a packet protocol, sometimes the packets get broken up. The ServerSocket is designed in such a way that when the Client sends data, part of the data contains the size of our message such as 55. Sometimes these packets are split into multiple pieces. They arrive in order but since we do not know how the messages will be split, our server sometimes does not know how to identify the split message.

So, having given you the background information. What is the best method to rebuild the packets as they come in if they are split? We are using C++ Builder 5 (yes I know, old IDE but this is all we can work with at the moment. ALOT of work to re-design in .NET or newer technology).

+4  A: 

TCP guarantees that the data will arrive in the same order it was sent.

That beeing said, you can just append all the incoming data to a buffer. Then check if your buffer contains one or more packets, and remove them from the buffer, keeping all the remaining data into the buffer for future check.

This, of course, suppose that your packets have some header that indicates the size of the following data.

Lets consider packets have the following structure:

[LEN] X X X...

Where LEN is the size of the data and each X is an byte.

If you receive:

4 X X X
[--1--]

The packet is not complete, you can leave it in the buffer. Then, other data arrives, you just append it to the buffer:

4 X X X X 3 X X X
        [---2---]

You then have 2 complete messages that you can easily parse.

If you do it, don't forget to send any length in a host-independant form (ntohs and ntohl can help).

ereOn
+1  A: 

I would have a function called readBytes or something that takes a buffer and a length parameter and reads until that many bytes have been read. You'll need to capture the number of bytes actually read and if it's less than the number you're expecting, advance your buffer pointer and read the rest. Keep looping until you've read them all.

Then call this function once for the header (containing the length), assuming that the header is a fixed length. Once you have the length of the actual data, call this function again.

Graeme Perrow
+1  A: 

This is often accomplished by prefixing messages with a one or two-byte length value which, like you said, gives the length of the remaining data. If I've understood you correctly, you're sending this as plain text (i.e., '5', '5') and this might get split up. Since you don't know the length of a decimal number, it's somewhat ambiguous. If you absolutely need to go with plain text, perhaps you could encode the length as a 16-bit hex value, i.e.:

00ff <255 bytes data> 000a <10 bytes data>

This way, the length of the size header is fixed to 4 bytes and can be used as a minimum read length when receiving on the socket.


Edit: Perhaps I misunderstood -- if reading the length value isn't a problem, deal with splits by concatenating incoming data to a string, byte buffer, or whatever until its length is equal to the value you read in the beginning. TCP will take care of the rest.

Take extra precautions to make sure that you can't get stuck in a blocking read state should the client not send a complete message. For example, say you receive the length header, and start a loop that keeps reading through blocking recv() calls until the buffer is filled. If a malicious client intentionally stops sending data, your server might be locked until the client either disconnects, or starts sending.

Martin Törnwall