| CPSC 124 | Introduction to Programming | Spring 2007 |
In this lab, you'll learn how to write programs that read and write files, as well as programs that communicate over the network. This lab will prepare for you the first programming assignment (which is due on 3/29). Both the lab and programming assignments are largely based on a lab by Scotty Orr and Stina Bridgeman. Note: this week's lab was intended to be short to give you added time to work on the programming assignment. Finish the lab as soon as you can, so you can start early on the programming assignment, which will take a significant amount of time. Also, make sure you read the introduction before starting the exercises. It will be crucial for both this lab and the first programming assignment.
Create a lab08 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s07/cs124/labs/lab08/ to your lab08 directory.
Reread pages 43 and 44 of the book (chapter 2) that cover file IO. If you don't have your book with, the book can be found online at http://math.hws.edu/javanotes5/.
One of the nice features of Java is that it is relatively easy (at least compared to other programming languages) to write programs which communicate over the network. The details of network communication, however, are beyond what we've covered at this point, so instead you'll be using a class called NetworkTextIO to handle sending and receiving data.
Network programs consist of two parts: a server program which listens for a connection request, and a client program which makes a connection request to the server. The programs can communicate once the connection is established, until the connection is closed. This is very much like using a telephone - the server is the person sitting by the phone waiting for it to ring, and the client is the person who dials the phone on the other end. Once the phone rings and the server picks up the receiver, the two parties can talk. The connection ends when one of the parties hangs up the phone. With phones, the person dialing needs to know the telephone number of the person receiving the call. A similar requirement exists with network programs - the client needs to know the hostname or IP address of the computer where the server program is running. Both parties also need to agree on a port number - you can think of the network connection to a computer as being like a big company which has one phone number and each employee has a different extension. The hostname/IP address is the phone number, and the port number identifies the extension.
A small example of server and client programs can be found in the files NetworkServer.java and NetworkClient.java, which you copied to your lab08 directory. The first thing you do should be to try out these programs. You'll need to cooperate with one of the other students in the lab - one will run NetworkServer and the other will run NetworkClient. (Alternatively, you can open two terminal windows and run one program in each window.) Here is what you should do:
First, one person should run the server (NetworkServer). It will ask for a port number - you can choose any number between 2049 and 65535. (If someone is already using that port, you'll get an error. In that case, just restart the program and pick a different port.) After you enter a port number, the computer will listen for a connection from a client. (The program will print a message saying it is waiting for a connection, and then look like it is just sitting there.)
Next, the other person should run the client (NetworkClient). It will ask for the name of the computer you want to connect to and a port number. The name should be the name of the computer where the NetworkServer is running - you can find that out by typing the command hostname in a terminal window on the same computer where the NetworkServer program was started. The port number should be the same number that was used when NetworkServer was started. Once the program gets the name and port number, it will send a connection request to the server program and the connection will be opened.
Once the connection has been opened, the client program will send the text "foo" to the server.
When the server gets the message from the client, it will print the message that the client sent and will then send the text "goo" back to the client.
The client will display the response it gets, and both programs exit.
You should now open NetworkServer.java and NetworkClient.java in your favorite text editor so you can see how they work. Both programs use several routines from the NetworkTextIO class. NetworkTextIO is very similar to TextIO. It provides all of the put, putln, get, and getln subroutines that TextIO does, with the difference that "put" now sends the data to the other end of the connection (instead of printing it on the screen) and "get" reads data from the other end of the connection (instead of from the keyboard). As is the case with keyboard input, data which is sent by using "put" on one end of the connection will not be available for reading with "get" on the other end of the connection until a newline is sent, either by explicitly including the special newline character "\n" in the String you give to put or by using putln instead. This is similar to how a program won't process input from the keyboard until you press enter. The simplest thing to do is just to always use putln instead of put.
NetworkTextIO also contains a few additional subroutines needed to setup and manage the network connection to another computer:
NetworkTextIO.listen(port) causes the program to act as a server and listen on the specified port. The port should be an integer between 2049 and 65535. (Lower port numbers are reserved for standard network services.) The program will wait until a client connects to it before continuing.
NetworkTextIO.connect(host,port) causes the program to act as
a client and send a connection request to the specified server.
host should be a String value which gives the Internet name or IP
number of the computer where the server is running. port is an
integer specifying the port number on which the server is listening.
NetworkTextIO.close() closes the connection if one is open. After the connection has been closed, no more data can be sent over the connection. After closing one connection, it's OK to open another connection by calling NetworkTextIO.listen() or NetworkTextIO.connect() again.
NetworkTextIO.isOpen() has a value of true if a connection is currently open, and false if no connection is open. An error will be produced if you try to put or get data if the connection is not open.
Make sure you understand both NetworkServer.java and NetworkClient.java before continuing with the lab.
Here are the exercises for this week's lab, due in two weeks (because of spring break). These will help you get started on the first programming assignment, so try to finish the lab as early as possible (it is a shorter lab).
Write a program called CopyFile.java that copies one file to another file.
It should prompt the user for a file name to read from and a file name to write to,
and then write the contents of the read file to the write file. You will need to
use some new methods in TextIO in order to do this: TextIO.readFile(
Once you think you have your program working, try copying the provided file 'foo.txt' to 'goo.txt'. Run your program, entering 'foo.txt' for the read file name and 'goo.txt' for the write file name. After the program completes open up the two files in emacs and verify that they are the same.
Now try entering in a bad file name (a name of a file that doesn't exist) for the read file. What happens? Add a try...catch statement to your program to catch this event. When a bad file name is entered for the read file, your program should print out an error message and then immediately exit the program using the System.exit(1) statement.
Now try entering the file name ~mcorliss/foo.txt, which you don't have permission to write to. What happens? Add a try...catch statement to your program to catch this event. When a bad file name is entered for the write file, your program should also print out an error message and then immediately exit the program using the System.exit(1) statement.
Verify that your lab08 folder contains all of the files you created or modified for this lab, then copy your entire lab08 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).