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
-
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. |
-
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.
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
Search within the programming environment to access the following installed examples:
-
Simple TCP
-
TCP Named Service
-
TCP Multiple Connections