Table Of Contents

Transferring Data over a Network Using TCP

Version:
    Last Modified: March 8, 2017

    Use Transmission Control Protocol (TCP) to reliably transfer data between a client and a server.

    To transmit data using TCP, you must develop the code to send and receive data on both the client and the server.

    What to Use

    What to Do

    1. Create the following diagram in a VI targeted to the host processor. This diagram illustrates TCP code on the server side.

      Customize the gray sections for your unique programming goals.

      The TCP Listen node waits for an incoming connection on a specific port. The port you specify on the server must match the port you specify on the client.
      TCP Read nodes read data from the specified port and retrieve commands from the client. You must specify the number of bytes to read. Make sure to read commands in accordance with your message handling strategy. For example, if a size prefix precedes a command, read the length first and then read the amount of data the length indicates.
      TCP Write nodes write data to the specified port and send responses to the client. Make sure you send data in accordance with your message handling strategy. For example, if the client reads a response prefix to know how many bytes are in the response, make sure to prepend the length of the response to the response.
      If any unread data remains and the connection closes, you may be unable to access that data. Make sure to read all data the client sends before closing the connection.
    2. Create the following diagram in a VI targeted to a processor, such as a real-time controller or a remote PC. This diagram illustrates TCP code on the client side.

      Customize the gray sections for your unique programming goals.

      The TCP Open Connection node opens a connection to the server at the port and address you specify. The address must match the IP address of the server, and the port you specify on the client must match the port you specify on the server.
      TCP Write nodes write data to the specified port and send commands to the server. Make sure to format commands according to the messaging strategy for your application, as described in the next table row.
      TCP Read nodes read data from the specified port and retrieve responses from the client. You must specify the number of bytes to read.

      Use one of the following strategies to handle messages of different sizes:

      • Precede each command and response with a fixed-size prefix that indicates the size of the rest of the command or response you want to send. This allows the client to read the number of bytes in the command or response first and then specify the appropriate number of bytes to read for the rest of the command or response.
      • Make each command and response a fixed size. When a command or response is smaller than the size you specify, you can pad it to the fixed size. This option is the most efficient but isn't as flexible as the previous option.
      • Follow each command and response with a specific terminating character. Read data in small units until you reach the terminating character.

      You can display the response data from the server on the client UI or further process the response data locally.

      If any unread data remains and the connection closes, you may be unable to access that data. Make sure to read all data the server sends before closing the connection.

    After you finish developing your server and client applications, your client and server applications will communicate using TCP when you run the applications simultaneously. Be sure to run the server application first to ensure that TCP Open Connection on the client is able to locate the listener.

    Troubleshooting

    • If you have trouble establishing a connection, try the following troubleshooting strategies:
      • Make sure the TCP Listen node on the server executes before the TCP Open Connection node executes on the client. If TCP Open Connection executes first, it will return an error because it cannot locate the listener.
      • Verify that the IP address you specify for TCP Open Connection on the client matches the IP address of the server. Note that different IP addresses that refer to the same computer are not compatible. For example, if you create a connection that listens to localhost and write to the numeric IP address of the local computer, such as 10.113.0.53, the client and server do not establish a connection.
      • Verify that the port you specify for TCP Open Connection on the client matches the port you specify for TCP Listen on the server.
      • Verify that the ports you have specified are valid, open ports. Valid TCP ports range from 0 to 65535. To dynamically locate an open port, use port 0 and provide a service name for any TCP Open Connection and TCP Listen nodes as illustrated by the images above.
    • If you have trouble transferring data after establishing a connection, make sure the bytes to read input on each TCP Read node receives a value greater than zero. If you wire a value of zero or leave the input unwired, the node does not read anything and does not produce an error.

    Examples

    Use Search to find and open the following installed examples:

    • Simple TCP
    • TCP Named Service
    • TCP Multiple Connections

    Recently Viewed Topics