cs3157
spring 2003
homework #4
due monday april 21, by 6am (electronically)
last modified:
Thu Apr 3 23:13:44 EST 2003
(sklar)
goal.
The goal of this assignment is to learn how to connect a Java program
with a C program via stream sockets using TCP.
You'll also get to write a makefile, lucky you.
This assignment is worth 10 points.
Distribution of points is indicated below.
In this assignment, you will take the Java front-end of your game that
you wrote for hw#1 and connect it to the C server that you wrote for hw#3.
You need to begin implementing the game, beyond the point of logging in
the user.
You probably will not need to write too much new code for your server.
You will mostly combine code you wrote for hw#2 and hw#3.
You will also have to add to the server the ability to handle the game
commands that your Java client will send to it.
The login handling code in the server should remain the same from hw#3
(unless you have things to fix leftover from hw#3...).
As with hw#2, remember to separate the functionality appropriately between the
server and the client:
- Any part of your code that interacts directly with data
files must be incorporated into the server.
Any functions you wrote that open and close, read or write to the
three data files from hw#2 (e.g., "domain.dat", "users.dat" and
"sklar.dat") need to go in the server.
- Any part of your code that interacts directly with the user must be
incorporated into the client.
So any functions you wrote that prompt the user for input, read the
user's input and write output to the screen need to go in the client.
Of course, you'll have to rewrite these in Java (from your earlier C versions).
You will now need to implement the full language that you have defined
for your client and server to communicate with each other.
First, get the login functionality written in the Java client and make
sure that the LOGIN, NEW, BYE, OKAY and BAD commands work (from hw#3).
Then implement the game-related commands.
For this assignment only, you do not need to submit any documentation.
This is because this assignment is heavy on programming.
You will need to submit documentation with hw#5, documenting the
source code as well as providing user documentation (i.e., instructions
to the user for how to play the game).
You can begin working on this but you don't need to submit it until hw#5.
PLEASE follow the file naming instructions, since this makes life so much
easier for the TAs.
(And makes them friendlier when grading, hint, hint.)
FIRST: print out the timesheet and keep track of how much time
you spend doing the tasks outlined below.
BEGIN by predicting the total amount of time that each task will take,
BEFORE you start working on the assignment.
Enter your prediction as the first row on your timesheet.
Then, as you work on the assignment, enter the actual data on the
subsequent rows.
See how closely (or not!) your predictions match the actual data.
1. build the Java client.
(6 points)
Start with my Client.java sample
program, your hw#3 C client and your hw#1 Java program.
Translate the functionality of your C client into Java.
The Java client should do the following:
- As in the sample client: (0.5 points)
- Get the port number from the command line.
- Create a socket and connect to the server using the host name
"localhost" and the second port number output by the server,
specified on the command line.
- Call a login method that prompts the user for her username and
password and reads her username and password.
(0.5 points)
- Send the username and password from the client to the server,
for validation, using either the LOGIN or the NEW command.
(0.5 points)
- Wait until the server responds, with either an OKAY or a BAD command.
(0.5 points)
- If the server sends a BAD command, then print an error message
and loop back to step 2, or create a new user (by sending a NEW
command).
When creating a new user, the server will respond with an OKAY
command.
(0.5 points)
- Once the server responds with an OKAY command, then the client
is ready for the game stage of the program.
Start by getting any pre-game options from the user.
(0.5 points)
- Send a request to the server for game data and wait to receive it.
(0.5 points)
- After receiving the game data from the server, parse the game data
and use it to initialize the game.
(0.5 points)
- Play the game...
This may involve passing data back and forth between the client and
server, in the case that the server needs to validate the user's input.
For example, if you are implementing a word game, you might need to check
the user's input against a dictionary. You will not keep the dictionary
in the client, of course, so you'd need to pass the user's input to the
server so the server can check the dictionary and then pass a validation
back to the client.
In any case, this will involve work on the user interface of your client,
so the user can play the game.
(1.0 points)
- When the game is over, send the game results to the server.
(0.5 points)
- Let the user play again or quit.
If the user decides to play again, then loop back to step 5.
If the user decides to quit the game, send a BYE command to the server and exit.
(0.5 points)
Name your client:
hw4client<your-cs-username>.java
,
so for example, mine is called hw4clientsklar.java
.
2. modify the server.
(2 points)
The server should already do the following (from hw#3):
- Establish a socket:
- Create a socket and bind it to a port (assigned by the system).
- Listen on the socket for connections.
- Get the port number assigned by the system and output that to the
screen (so you'll know the number for when you start up the client).
- Accept a connection from the client.
- Wait for input from the client, and when a message is received,
parse it and process it.
- When a LOGIN command is received, the server should check if the
username and password match what is in the "users.dat" file.
If it matches, then the server should send an OKAY command to the client.
If it doesn't match, then the server should send a BAD command to the
client.
- When a NEW command is received, the server should act like in
hw#2 where you create an entry in "users.dat" and a data file for the
new user. Then the server should send an OKAY command to the client.
- Then the server should wait for the client to finish executing.
The client will send a BYE command, so the server should wait until it
receives one.
Then the server can exit.
Now you need to modify the server to handle the game commands that your
client is now sending.
This means that the server must:
- Read the domain knowledge data file ("domain.dat") that you defined
for hw#2, to get domain data for the game (0.5 points).
- Send the domain/game data to the client (0.5 points).
- Read the game results from the client (0.5 points).
- Write the game results to the student profile data file (0.5 points).
Name your server:
hw4server<your-cs-username>.c
,
so for example, mine is called hw4serversklar.c
.
3. write a makefile.
(1 point)
Write a makefile that will compile both your server and your client.
We will go over the use of the Unix make utility in class on
Monday April 7.
Name your makefile:
hw4<your-cs-username>.mk
,
so for example, mine is called hw4sklar.mk
.
4. submit.
(1 point)
You need to submit your program in two ways: electronically and hard copy.
- electronically
Follow the submission instructions.
hardcopy.
(1) Print out the homework template cover page and fill it out.
(2) Print out the timesheet and fill it out.
(3) STAPLE the cover page and timesheet together.
(4) Make sure your name and CS username are on all the papers you hand in!
(5) Bring all this to class on the day the assignment is due and
deposit it in the homework box at the front of the classroom within
the first 5 minutes of the class (by 1.15pm).