Lab 15
Readers and Writers for BankAccount Transactions over Sockets
Introduction
During Lab14, we looked at how we might "load" a BankAccount by reading
transactions from a text file and then "saving" the BankAccount by writing it
back out to a (different) text file.
In class since Lab14 we have been talking about using sockets as a means of
communicating over a network and the fact that sockets allow us to send entire
an Object as long as it implements the "serializable" interface.
This week you are going to walk through an activity very similar to what you
did last week in Lab 14 but this time taking advantage of Sockets.
To begin with you will need to download and unzip the following starter code
If you look at the code in this distribution you will see two sets of code.
- The first set is in a directory named "server" This SHOULD be
completely working code that runs a simple server like that held at a bank.
This server accepts connections from ATMs (more in a bit). After
completing the connection process with an ATM, the server assumes that the
first thing that will happen is that it will receive a String object
representing either the String "Read" or "Write"
- If the String is "Read" then the server opens up a text file, reads the
Transactions from the text file and sends them one Transaction at a time to
the ATM. When the server reaches the end of the text file it sends the
null object to signal that it is done sending to the ATM and closes the
connection.
- If the String is "Write" then the server opens up a different test file
from writing and starts to receive Transactions from the ATM. Each
Transaction received is written to a text file. When the Server
receives the null object, it closes the text file and closes the connection
to the ATM.
- The second set of code is in a directory named "client." This code
is based on your ATM code from Lab 10. It is most of the code needed to
complete this application. However, two classes are still missing.
Your task will be to write those two missing classes
In order to avoid errors, you should compile the code in each directory by
invoking "javac *.java" within each directory before beginning and from time to
time as you make changes.
Tasks
Complete this object-oriented program by doing the following:
- Within the Client directory, write the
BankAccountNetworkReader class. A
BankAccountNetworkReader is an object that will read Transactions off
of a socket connection to the server and save these into a BankAccount.
The BankAccountNetworkReader class should
provide:
- a constructor that takes a BankAccount (presumable empty at this point)
and the port number the reader will be reading from.
- a read() method that
- opens a socket to the given port
- Sets up ObjectInputStream and ObjectOutputSteam for communication over
the socket (see session 38
notes)
- Sends the server the String "Read" to signal that it wants to read from
the server
- Reads objects from the server until it receives a null object. It
is assumed that each object received from the server will be either one of
our three Transactions (in which case it is added to the BankAccount) or the
null object (in which case it can quit reading).
You will be running both the client and the server on your machine, so use
InetAddress.getLocalHost() as the server value.
- Within the Client directory, write the
BankAccountNetworkWriter class. A
BankAccountNetworkWriter is an object that will read a local
BankAccount and write its Transactions on a socket connection to the server
The
BankAccountNetworkWriter class should
provide:
- a constructor that takes a BankAccount (presumable with information) and
the port number the writer will be writing to.
- a write() method that
- opens a socket to the given port
- Sets up ObjectInputStream and ObjectOutputSteam for communication over
the socket (see session 28 notes)
- Sends the server the String "Write" to signal that it wants to write to
the server
- Gets the transactions from its BankAccount, writes these Transaction
objects one at a time to the server, and finally sends the null object when
it has finished.
When you are ready to test this, open one window and run the Server (BankAccountServer)
and open another window and run the Client (BankAccountClient). Add two or
three transactions to the BankAccount using the ATM window and then press the
"write" button. Open the file named "transactions2.txt" (located in the
server directory). and verify that this file contains all of the transactions
from the original file (transactions1.txt) plus the new ones you added using the
ATM window.
Deliverables
Clean out all of the *.class and *.java~ files then zip all of these together into a single Lab15.zip file and submit via
submission system.