TCP Read Function

Owning Palette: TCP VI and Functions

Requires: Base Development System

Reads a number of bytes from a TCP network connection, returning the results in data out.

Example

 Add to the block diagram  Find on the palette
mode indicates the behavior of the read operation.

0Standard (default)—Waits until all bytes you specify in bytes to read arrive or until timeout ms runs out. Returns the number of bytes read so far. If fewer bytes than the number of bytes you requested arrive, returns the partial number of bytes and reports a timeout error.
1Buffered—Waits until all bytes you specify in bytes to read arrive or until timeout ms runs out. If fewer bytes than the number you requested arrive, returns no bytes and reports a timeout error.
2CRLF—Waits until all bytes you specify in bytes to read arrive or until the function receives a CR (carriage return) followed by a LF (linefeed) within the number of bytes you specify in bytes to read or until timeout ms runs out. The function returns the bytes up to and including the CR and LF if it finds them in the string.
3Immediate—Waits until the function receives any bytes from those you specify in bytes to read. Waits the full timeout only if the function receives no bytes. Returns the number of bytes so far. Reports a timeout error if the function receives no bytes.
connection ID is a network connection refnum that uniquely identifies the TCP connection.
bytes to read is the number of bytes to read. You must wire a value greater than 0 in order to read data from a network connection. The default is 0. Use one of the following techniques to handle messages that might vary in size:
  • Send messages that are preceded by a fixed size header that describes the message. For example, it might contain a command integer that identifies what kind of message follows and a length integer that identifies how much more data is in the message. Both the server and client receive messages by issuing a read function of eight bytes (assuming each is a four byte integer), converting them into the two integers, and using the length integer to determine the number of bytes to pass to a second read function for the remainder of the message. Once this second read is complete, each side loops back to the read function of the eight byte header. This technique is the most flexible, but it requires two reads to receive each message. In practice, the second read usually completes immediately if the message is written with a single write function.
  • Make each message a fixed size. When the content of a message is smaller than the fixed size you specify, pad the message to the fixed size. This technique is marginally more efficient because only a single read is required to receive a message at the expense of sending unnecessary data sometimes.
  • Send messages that are strictly ASCII in content, where each message is terminated by a carriage return and linefeed pair of characters. The read function has a mode input that, when passed CRLF, causes it to read until seeing a carriage return and linefeed sequence. This technique becomes more complicated when message data can possibly contain CRLF sequences, but it is quite common among many internet protocols, including POP3, FTP, and HTTP.
timeout ms specifies the time, in milliseconds, that mode waits before reporting a timeout error. The default is 25,000 ms. A value of –1 indicates to wait indefinitely.
error in describes error conditions that occur before this node runs. This input provides standard error in functionality.
connection ID out returns the same value as connection ID.
data out contains the data read from the TCP connection.
error out contains error information. This output provides standard error out functionality.

Example

Refer to the TCP Named Service.lvproj in the labview\examples\Data Communication\Protocols\TCP\TCP Named Service directory for an example of using the TCP Read function.

 Open example  Find related examples