Transferring Data with TCP
TCP is a stream-oriented, binary data transfer protocol. TCP does not have any special built-in capabilities for data types such as floating-point numbers, structures, arrays, and so on. The streaming nature of the protocol results in data being fragmented and coalesced among packets. Therefore, if you send 100 8-byte double-precision numbers in one write call from a client, the server might not get all the 100 8-byte double-precision numbers in one read call. Furthermore, because TCP is not aware of data types, a read call might not even receive the data in 8-byte boundaries.
You must design the reading code to handle such cases. The data length is not an issue if both the reading code and the writing code know the size of each data transfer because the reading code can keep reading the data until all the bytes are received. However, writers sometimes must send data of varying lengths. In these cases, you can send the data in a message with size information encoded in the header of the message. For example, consider that each message has a 1-byte header that represents the number of eight-byte double-precision numbers following it in the message.
Refer to TCPCallback function in the MessageReader.c file of the samples\tcp\message.cws example for an example of code that reads messages in a server program's TCP callback function.
Because TCP is a binary data transfer protocol, all data is assumed to be in bytes. Therefore, you must consider issues such as byte order (little endian vs. big endian) when you send and receive data.
Streaming Nature of TCP
In TCP, because data is sent in a byte stream, it is not possible to know how many bytes are present in the data stream. Therefore, you cannot query the LabWindows/CVI TCP Support Library to get the amount of data available. When you call ServerTCPRead or ClientTCPRead, the library returns the number of bytes read into the data buffer. If the number of bytes read is the same as the data buffer size, then more data still might be present in the data stream. You can read the remaining data immediately by calling the read functions again or wait until the TCP callback is called again with the TCP_DATAREADY event. The LabWindows/CVI TCP Support Library calls the TCP callback with the TCP_DATAREADY event whenever events are processed and there is data in the TCP stream.
The ServerTCPWrite and ClientTCPWrite functions return the number of bytes written to the TCP stream, which might be less than the number of bytes in your data buffer. You must check for this condition and make sure all the bytes in your data buffer are written out successfully.
Refer to SendCallback function in the MessageWriter.c file of the samples\tcp\message.cws example for an example of code that checks for data in the TCP stream.
![]() |
Note A single-read function can receive multiple data messages or parts of messages. Therefore, the application that sends the data can indicate to the application that receives the data where the message ends by specifying the message size or passing a special character to indicate the end of a message. |