An Asynchronous Multi-client TCP Server in LabVIEW



First, please download the example code here: TCP Example

Next, open TCP.lvproj and then open and run “TCP” and “TCP Client” (do not run “TCP” or “TCP”, these VIs will be launched automatically).  Once both “TCP” and “TCP Client” are running, click the “Launch” button several times on the TCP Client Launcher VI to create TCP Clients that will connect to the Server.  The Server is listening for connections, so it will connect to the client and launch a “TCP Handler” VI to maintain that connection.  In a real application, you probably wouldn’t want to pop-up the front panel of each TCP Handler, they would just run in the background, but for this example I set the VI Properties to show the front panel when called so it’s easier to follow what’s happening.  If you type in a message in the TCP Client and click send, it will send that data to the associated TCP Handler and the TCP Handler will echo the message back, along with the Remote Port.  You can launch multiple clients and they will all connect simultaneously.  If you stop a client, the associated handler will automatically stop.  If you stop the server, all handlers will stop.


Lets take a look at the code, to see how this works.  Here’s the TCP Server:

TCP Server

The server simply listens for TCP connections in a loop, with a 1 second timeout.  If it time’s out, it does nothing and continues listening.  If it connects, it saves the connection in an array and asynchronously launches a TCP handler and passes the connection reference and remote port to the handler.  By the way, f you are wondering why I’m not using a strictly-typed VI reference to launch the handler, be sure to check out my other article that explains why here.  When the server stops, it closes all TCP connections in the array, which causes all of the TCP Handler VIs to stop.

Now let’s look at the TCP Handler:

TCP Handler

The TCP Handler code attempts to read a packet and, if it receives one, echos the received message back, along with the remote port number.  The handler continues to run until it receives an error other than a timeout error (code 56).  This ensures that the handler will close if the connection they are handling is closed externally (such as when the TCP Server or Client is stopped).  Some additional error handling will likely be needed for your specific application, but I wanted to keep this example simple for demonstration purposes.

Finally, let’s take a look at the client side VI:

TCP Client

The TCP Client VIs open, read, and write to the TCP socket when the user clicks “Send”.  Notice that the TCP Client does not need to know which handler it is connected to, it just simply opens a connection to the IP Address and Port and it connects.

Hopefully this article helped you to understand how to leverage the TCP socket’s multi-client feature.  As always, if you have any questions or suggestions please leave a comment below.  Thanks for reading!

1 thought on “An Asynchronous Multi-client TCP Server in LabVIEW

Leave a Reply


You are commenting using your account. Log Out /  Change )

Google+ photo


Twitter picture


Facebook photo


Connecting to %s