First Bytes Programming Lab 6

This lab is available online at www.cs.utexas.edu/users/scottm/firstbytes.

Introduction: In this lab you will create a more complex chat program that can actually communicate with other chat programs. In this lab you will start with a partially complete program and make the necessary changes and connections to create a working program.

  1. Download the file ChatSkeleton.vi  from the website www.cs.utexas.edu/users/scottm/firstbytes/

  2. Start LabView. Instead of creating a new VI choose the open option and open the ChatSkeleton program.

  3. This program is a little more complex than any you have worked with yet. You only need to make some additions and changes to the program to get it to work.

  4. First, notice there is no send button. Add an OK Button to the front panel. After placing the button on the front panel and changing its name to Send go to the block diagram and drag the button to inside the top while loop, the one that has the caption "Read message and send it to all other active chat programs" 

  5. Next put the parts of a message together. The parts of the message are the output of the Enter Text function and the output of the Your Name function. To make the message look like regular chat messages use a string concatenate function with the name first, then a colon ":" via a String Constant, and finally the contents of the Enter Text function.


  6. The output of the String Concatenation must be wired to the input on the Data Socket Write function. This function writes data to a connection between your program and other programs, which is the essence of chat type programs: sending data from one computer to another. The data socket functions in LabView deal with all the low level details of networking between different programs and different computers.


  7. The last step in building the program is to react to data being received from another program. The lower while loop is used to receive and display data from other chat programs. This loop has the explanation "Receive messages from this, and other chat programs. Display them in the window." above it. This loop is checking ever 0.3 seconds for new data. If no data is received the Data Socket Read function times out. There is an output from this function that is true if it timed out which means no data was received or false if it did not time out, meaning new data was received. This new data should be displayed in our Chat Window.

    Select the false option on the case structure. Drag the chat window icon into the middle of the false option on the Case Structure. Add a Concatenate Strings function and expand it so it can take in three inputs. Connect the output of the Data Socket Read to the bottom input of the Concatenate Strings function. Create a Carriage Return Constant and connect this to the middle input of the Concatenate Strings function. The partial result should look like this:


  8. We will use a slightly different method to continue to display the old contents of the Chat Window. Create an Empty String Constant and place it outside the bottom while loop.

  9. Right click on the left side of the while loop. Choose the Add Shift Register option. This create a flow of data between one iteration of the while loop and the next. Wire the Empty String Constant to the input shift register on the left. (The triangle that is pointing down.)


  10. Wire the shift register to the top input on the String Concatenation function. Wire the output of the String Concatenation function to the input of the Chat Window and to the output shift register, the up triangle pointing up on the right side of the while loop.

  11. Now select the true option on the bottom case statement. Wire the two connections on the true option together.



    The program should be finished. The final product should look something like this:


  12. The last steps are to set up LabView so it has the ability to communicate. Before the chat program can work one student in the lab needs to perform the following steps to configure a DataSocket server on their computer to act as a server. (A server is just a computer that provides services to other computers.) On that computer launch Programs>>Programming>>National Instruments>>DataSocket>>DataSocket Server Manager. The following window should appear.


  13. Create a new default data item called UTFirstBytes by clicking on the New Item button and make sure that the configuration matches the screenshot above. VERY IMPORTANT. Make sure the box entitled "Allow Multiple Writers" is checked.

  14. You also need to configure the security of the DefaultReaders and DefaultWriters to allow everyone to access the server. Select the DefaultReaders option and ensure the everyhost option is selected.


  15. Select the DefaultWriters option and ensure everyhost is selected.

  16. Make sure to save the changes to the server (Ctrl-S or use the Settings>>save settings now)

  17. Your program should now be ready to run. You can chat with yourself by saving the program with a different name and then running both versions of the program. You can also chat with someone on another computer. To access a server on another computer, you can change localhost to its machine name or the IP address. 

    A Chat Server is a DataSocket  Data item, identified at the top of the front panel by a URL (dstp://localhost/UTFirstBytes). Here dstp is the DataSocket protocol, localhost is the machine name of the server (in this case it is the same computer that is running the chat program) and UTFirstBytes is the name of the data item on the server.



    Change local host to the computer name of someone else in the lab that is running the chat program. You can find names by right clicking on the myComputer icon on the desktop and selecting Properties. On the computer properties window select the Computer Name tab. The name of the computers in the lab will be something like O7.


  18. What next.

    1. Try setting up a way to block messages from certain users.

    2. Try to send coded messages. A very simple code is a Caesar shift cipher. Pick a number and shift every character that amount. To decode your messages the receiver will have to know what value of a shift you used. (See the instructions below for more details on encrypting messages.)


Encrypting Messages

 One simple form of encryption involves shifting every character by a certain number of characters. This is know as a Caesar shift cipher because it was used 2000 years ago in the time of the Roman Emperor Caesar. If we only shifted letters and chose a shift 3 the word CAT would become, FDW. The C shifts to F, the A shifts to D, and the T shifts to W. If you had a Z and a shift of 3 the Z would become a C. We will shift all characters in our messages. To decode the message the shift is reversed.

1. Open your chat program. We will have to convert the text of our message to an array of numbers. An array is simply a list of values. Go to the Block Diagram. Select All Functions >> String >> String / Array / Path Conversion >> String to Byte Array

2. Delete the wire connecting the Concatenate String function that puts your message together and sends it to the Data Socket Write. Wire the output of the Concatenate String function to the input of the String to Byte Array function.

3. We want to display this array so we can see the transformation. Go to the front panel and select All Controls >> Array Cluster >> Array. There are two boxes that appear. The one with the control is used to choose the index to display of the array. Arrays are like lists and each element of the list is numbered. You need to add a Numeric Indicator to the inside box. After the numeric indicator is added change the format of it to Unsigned Byte. (U8). Only a single element of the array is shown. You can show more elements by expanding the single element to the right.

4. You actually need two array display so add another one of them. One should be called unencoded and the other encoded.

5. Go back to the block diagram. Wire the output of the String to Byte Array to the input of one of the Array displays.

6. To shift all the elements of the array that contains the numbers representing the characters of the message we will use a loop. Add a While loop to the inside of the case structure for sending messages. Change the While loop to a For loop. 

7. Inside the For loop place an Add function. Wire the output of the String to Byte Array to the input of the add function. The other input will be the shift. Right click on the other input to the Add function and say Create >> Control. Move the control outside the For loop and outside the Case structure then wire it to the input of the Add function.

8. Outside the For loop convert the array of unsigned bytes back to a string by using a Byte Array to String function. (this is on the same menu as the String to Byte Array function.) Wire the output of this function will be the input to the Data Socket Write.

9. Now on the receive message part the process must be reversed. The input string from the Data Socket Read must be converted to an array of unsigned bytes, all elements of the array must be shifted back, using subtract, and then converted back. You do not need array displays for this input. You will need a control for the shift back, one of the operands to the subtract function.

10. Now when you run your program you will select a shift. If someone wants to be able to makes sense of you message you must tell them (in a way other than the chat program itself) what shift to use.