CPSC 124 Introduction to Programming Fall 2006

Lab 6: File and Network IO

Introduction

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 10/23). 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.

Setup

Create a lab06 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/f06/cs124/labs/lab06/ to your lab06 directory.

File IO

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/.

Networking, Servers, and Clients

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 lab06 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:

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:

Make sure you understand both NetworkServer.java and NetworkClient.java before continuing with the lab.

Exercises

Here are the exercises for this week's lab, due next Thursday. These will help you get started on the first programming assignment, so try to finish the lab early (it is a shorter lab then in previous weeks).

  1. Write a program called ReadFile.java that prompts the user for a file name, then prints the contents of that file to the screen (use System.out.println to print to the screen not TextIO.putln). Hint: you can use the command TextIO.eof() to determine when you're at the end of the file. Try your program out using the file "foo.txt" that was included in the lab06 directory. Open "foo.txt" in emacs to verify your program works correctly.

    Once you have your program working, try entering in a bad file name (a name of a file that doesn't exist). What happens? Add a try...catch statement to your program to catch this event. When a bad file name is entered, your program should print out an error message and then immediately exit the program using the System.exit(1) statement.

  2. Write a program called WriteFile.java that prompts the user for a file name, and then writes the following text to the file:

    start
    a
    b
    c
    12345
    zoinks!
    *foo*
    eof
    

    Run your program, and open the output file in emacs to verify that it works correctly. Note: if you enter an existing file name at the prompt, your program will overwrite this file. So don't give it the name of an important file (i.e., do not enter the name "WriteFile.java").

  3. Modify NetworkClient.java and NetworkServer.java to prompt the user for the message to send to the other (rather than using "foo" in the client and "goo" in the server).

Handin

Verify that your lab06 folder contains all of the files you created or modified for this lab, then copy your entire lab06 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).