Sending and receiving data

From Overbyte
Revision as of 10:52, 21 February 2006 by Wilfried (talk | contribs) (BinaryData moved to Sending and receiving data)
Jump to navigation Jump to search

General

There are several ways to transfer data. No matter if it is a file an image ASCII or binary data it is all the same. What is needed is a certain format so that the receiver of the data knows he has a complete packet.


Many solutions are of course possible. You can use Line Mode or packet data. Using Line Mode has the advantage that TWSocket will concatenate packets for you but the disadvantage that it will hold all data in memory before it will fire OnDataAvailable. The latter is of course only important if packets are very large. If you choose for packet data then you have to concatenate data yourself but you have control on what is kept in memory or not.

Line Mode

Base-64

Base64 is an encoding process using a 64 letter alphabet where each letter representing 6 bits in the input stream. It is described in RFC 2045. All encoding / decoding procedures can be found in TMimeDecode component. The encoded data is about 33 percent larger and not human readable. Every character not used in the Base64 alphabet can be used as control character, including line end.

ASCII-hex

ASCII-hex is used in many protocols. Every character is converted into his hexadecimal equivalent and sent as such. For example the string '123' is sent as '313233'. The encoded data is twice as long and difficult human readable. Every character except 0..9, A..F can be used as control character, including line end.

Escaping

Escaping is very often used. Control characters including line end has to be chosen in a way they are as less as possible in the original data. The principle is to precede the control characters with an escape character and replace them by other characters. Very often a NULL character is escaped as well. The data is only a little longer than original and good human readable.

Examples

Packet data

Preceding each data packet with his length

This is a very common used technique. The first 1, 2 o 4 bytes of the data represent the length of the packet. Note that it is common habitude in communications to represent the length header in Big Endian format while Intel CPU use by design Little Endian format.

Less used but still practice is to give the preceding length in hex format of 2, 4 or 8 bytes. Advantage is that it part of the header is human readable.

Fixed length data

Mixed mode

Conclusion

Difficult to explain something :)