In Part I, we discussed the fundamentals of client-server applications through a simple temperature monitoring application. In Part II, we use LabVIEW code samples to review these principles and learn how to develop client-server applications.
Client-server applications must perform the primary functions discussed in Part I -- client programs request service from a server by sending it a message, and server programs process client requests by performing the tasks requested by clients. Review Part I.
With this basic understanding of client-server applications, let us look more closely at a temperature monitoring application. The server in this application continually detects requests from client applications, processes the requests, and responds by returning the current temperature to the client. Refer to the end of this article for the LabVIEW code illustrated in this article.
Please note that using TCP/IP methods may not be the most effective way to develop applications where data is being shared or distributed. You can develop applications faster and with built-in security options using simpler methods and features in LabVIEW, including DataSocket and VI Server. This article uses TCP/IP methods for the purpose of illustrating the concepts of client-server applications.
2. A Simple LabVIEW Client-Server ApplicationIn Part I, we defined a simple temperature monitoring application. We divided the application into three modules to make the application easier to maintain. Using while loops, you can implement the major components of the server application. These components are -- detecting temperature data requests, processing temperature data requests, and responding to requests. Your first loop executes until the server receives the connection request. The second loop continues to provide temperature data to the client until the client disconnects from the server.
Figure 1. Server Waiting for a Client Request
Before we give details, let us plan the client-server steps in our simple temperature monitoring application. The client and server follow these steps:
1. Client sends a request.
2. Server detects the request using the TCP Listen.vi.
3. Client connects to the server using the TCP Open Connection.vi.
4. Server uses the TCP Write.vi to send temperature data to the client.
5. Client reads the transmitted data using the TCP Read.vi.
More specifically, here are the actions the server takes when detecting a client request. The server detects a client request with the TCP Listen.vi, found in the TCP utility library (<labview-dir>\vi.lib\Utility\tcp.llb). The VI creates a listener which waits for an accepted TCP connection from a client. This is similar to the way your telephone operates. The telephone in your house waits or listens for a call or connection from another phone. The TCP Listen.vi enables your server to wait or listen for a connection from a server. The loop, illustrated in Figure 1, waits for a connection from a client and processes any errors or warnings from the TCP Listen.vi. Error checking and support of an abort mechanism is not necessary for the application, however it is generally good programming practice because you can handle different errors appropriately and the server operator can quit the application in an emergency.
Once the server detects the client request, the client connects to the server with the TCP Open Connection.vi, found in the Communication-TCP function palette. The client connects to a specific server application by specifying the server address -- the IP address or hostname -- and the port.
Figure 2. Client Opening a Connection
When the connection between the client and server is made, the server sends temperature data to the client, and the client reads and displays the returned data. TCP Write.vi and TCP Read.vi primarily pass data between the client and server.
In Figure 3 and 4 below, we illustrate the associated code for the data transfer between the client-server applications.
Figure 3. Server Sending Temperature Data to the Client
Figure 4. Client Reading Temperature Data from the Server
Our temperature monitoring application is fairly simple. However, in larger applications, the size of the data you request from the server can put limitations on your application. Therefore, before sending the actual temperature data, the server (Figure 3) sends the size of temperature data being transferred. This mechanism is necessary in applications where a server sends an array or other complex data structure of unknown size. This also ensures that no data is missed.
Refer to the Understanding Client-Server Applications -- Example Code to investigate these concepts. Refer to the "SERVER - SIMPLE.vi" and "CLIENT.vi".
3. Client-Server Communication
Client-server applications are not limited to transferring data. Many times, you need to run an application that synchronizes the client-server application, indicates the state, or identifies and authenticates clients. If you need this type of data for your application, use the TCP Read.vi and TCP Write.vi to read and write data between the client and server. You must predefine any information you need to provide this added functionality.
4. Streaming Temperature Data
In a simple situation, you may require a client to query for each temperature point one at a time. In Figures 3 and 4, the client connects once to the server and continuously receives temperature data until the client is stopped.
The client stops the data transfer by sending a byte of data ("Q") to the server (Figure 4). The server tries to read data the client sends. If it detects that only one byte of information has been sent, the server knows the client has requested a termination of the connection. If you did not predetermine this relationship, the server would continue to send temperature data to the client.
5. Authenticating/Validating Clients
Some applications and environments require that you authenticate a client before a server can return data. If you are concerned about sensitive or confidential information, it is important to only service authorized clients. You can validate clients as a separate module to easily incorporate it into the client-server relationship. Review
Part I of this article for more information on modular client-server architectures.
Figure 5. Validating Client Access to Server Data
You can authenticate clients using an access control list, or a list of authorized clients, on the server machine. The TCP/IP Access Validation -- refer to the TCP/IP Access Validation link below -- checks clients against this list to validate them.
Alternatively, you can use passwords to validate a client. After connecting to the server, the client sends a password and other identifiable information to the server using TCP Write.vi. The validation module in the server reads the password sent by the client and validates it. If the password is incorrect, the server denies access to the client.
Figure 6. Password Validation Module
In Figure 6, we illustrate a simple password authentication mechanism. Here, the client provides a password which is verified against a list of acceptable passwords maintained by the server.
Refer to the Understanding Client-Server Applications -- Example Code to investigate password protection. Refer to "SERVER - wPASSWORD.vi" and "CLIENT - wPASSWORD.vi".
TCP/IP Client Access Validation
6. Security Considerations
We have covered basic client validation. However, what if you have a more in-depth security concern? How do you implement additional security measures in a client-server application? We point out some additional considerations; this is not an exhaustive discussion of security in client-server applications.
When you use access control list or passwords to authenticate a client, you transfer the password in the "open". Refer to the example code at the end of this article. You have not encrypted or protected the password, leaving it open to a malicious application, which can monitor network traffic to obtain the password. The malicious application has compromised the password and can use it to access data from the server. For this reason, it is important to protect passwords and other data used for authentication through encryption, reducing the risk of the password being compromised.
In addition to encrypting passwords, sometimes you need to encrypt all data transferred between the client and server. For example, you may want to encrypt your data if your application is distributed to a client or server outside your company firewall.
7. Serving Multiple Clients with LabVIEW
Many applications require more than one client to access data from the server. The server must handle multiple connections and respond to each and every client requesting service. The server can respond in a First-In-First-Out (FIFO) manner or handle multiple clients simultaneously -- in parallel.
When you try to handle multiple clients with one server, you can realize the benefits of modular programming. Your overall structure remains similar to those we have already discussed. Let's refer back to our temperature monitoring application. When serving multiple clients, you need to separate the temperature acquisition portion of the application from client requests. Temperature acquisition runs independently of responses to client requests.
The servers that handle multiple clients use VI Server to control the different modules of the server application, including the temperature acquisition module. This article does not discuss VI Server, however, Part III or this series introduces you to the basics of using VI Server. Refer to the communication examples installed with LabVIEW as well as the VI Server examples and tutorials in NI Developer Zone for further information on using VI Server.
The server application uses a queue to handle multiple clients. A queue data structure adds and removes elements in a FIFO manner. If you add element "a" followed by element "b", they are handled in the queue in that order. You can find VIs for queue's in the Advanced - Synchronization function palette. Refer to Figure 7 to see the difference between the simple server (Figure 1) and a FIFO server. In both architectures, a client request is detected in the same manner. Figure 1 processes that client request only, and during this time does not respond to any other clients. However, if you are using an FIFO queue, each client is handled in the order it arrives.
Figure 7. Queuing Client Request
Figure 8 uses a while loop to send data to clients and the queue to determine if any client have made requests.
Figure 8. Handling Queued Client Requests -- A While Loop
With this mechanism, the server responds to clients in a FIFO manner. A FIFO response mechanism fails if an individual client request takes a long time, starving other clients of service. In this case, the server must impose a limit on the time an individual client can receive service or handle clients simultaneously or in parallel.
Refer to the Understanding Client-Server Applications -- Example Code to investigate a FIFO Server. Refer to "SERVER - FIFO.vi".
8. Serving Multiple Clients in Parallel
Handling multiple clients in parallel can be tricky in a dataflow environment. The only architectural difference is choosing to serve clients in FIFO or in parallel.
Figure 9. Handled Queued Client Requests in Parallel
In parallel, the server creates a copy of the SubVI used to respond to a client. This is the same SubVI used in the FIFO server. It then calls this copy by reference and runs it. The copy of the VI responds to the corresponding client and continues to run until the client stops running. The server creates a new copy of the SubVI for each client request. In this way, it can call each copy independently and handle multiple client requests in parallel.
Refer to the Understanding Client-Server Applications -- Example Code to investigate a Parallel Server. Refer to "SERVER - PARALLEL.vi".
The clients in the above examples receive a subset of the temperature data collected by the server application. The server only returns the temperature data currently collected by the server. Therefore, it does not provide the client with any data between the last transfer of data. To provide all data to a client, the server must buffer the data, keep track of what data has been provided to each client, and transfer all the buffered temperature data to each client.
In Part I, you learned the basic nature of client-server applications. The client requests service and the server process client requests and responds to them. This basic principle remains constant throughout the different server architectures. However, the complexity of the server varies depending on the particular application requirements.
Tune in for Part III of this series as we explore DataSocket and VI Server.
Understanding Client-Server Applications -- Example Code
Understanding Client-Server Applications -- Part I