This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

TCP call back function and packet sequence numbering

In the RL library TCP callback function, the parameters we have are:

soc - Socket number
evt - Event (enums defined in Keil's rtl.h)
*ptr - Received data frame
par - Received data frame length

What I have realised is that the callback parameters don't provide us with any information on TCP packet sequence numbering. Now, my understanding is that TCP packets aren't guaranteed to be received in order, and I also understand that there is no guarantee of a 1:1 relationship between packets transmitted, and those received. This might happen because, for example, the packet hits a network device with a lower MTU resulting in the packet being split; or perhaps two packets are concatenated at some point.

For this reason, my understanding is that TCP must be considered a stream; and that at my application level protocol, I need to implement a means of identifying - at receiving end - that a complete application protocol packet has been fully received. An obvious way of doing this would be to have a header in my application packet that contains a packet length. At the receive end, I would keep reading data in TCP_EVT_DATA of the callback and rebuild the application packet locally until I have the required message size. I would still have to do this even if I guaranteed that my packets were 'small' enough to fit into a single TCP packet at transmit end, because as stated above it's a stream; there's no way to guarantee a single packet out for every packet shoved in (even though, most of the time, it probably is 1:1).

A further requirement, in order to be very robust, is to also look at the TCP packet sequence numbering to ensure that the application packet is rebuilt without any parts in the wrong order. But as the RL-Library callback doesn't provide this, do I just have to assume that it's unlikely that TCP will not deliver packets in order? To protect against that happening, would I be best using a CRC check on my entire reassembled packet?

Or, does the Keil libary already just deal with this for me, and guarantee packets delivered to the callback in order? I'd have thought that's most unlikely, considering the resource limitations.

Any thoughts are appreciated - even if it's to tell me I'm barking up the wrong tree.

Regards

Trev

Parents
  • From the application's point of view it is quite simple.

    The sender sends a stream and the receiver receives the stream. The receiver receives it in the same order as was transmitted.

    The TCP protocol stack looks after the sequence/acknowledgement numbers, retransmissions, ensuring correct packet order etc.

    One thing that you must bear in mind is that you should not rely on a block being received with the same size as that sent. TCP is stream oriented, not block oriented. So if you want to communicate blocks, the stream must contain something that allows the receiver to re-construct a transmitted block. A typical approach would be to send the block length followed by the block data.

Reply
  • From the application's point of view it is quite simple.

    The sender sends a stream and the receiver receives the stream. The receiver receives it in the same order as was transmitted.

    The TCP protocol stack looks after the sequence/acknowledgement numbers, retransmissions, ensuring correct packet order etc.

    One thing that you must bear in mind is that you should not rely on a block being received with the same size as that sent. TCP is stream oriented, not block oriented. So if you want to communicate blocks, the stream must contain something that allows the receiver to re-construct a transmitted block. A typical approach would be to send the block length followed by the block data.

Children