due monday april 21, by 6am (electronically)
Thu Apr 3 23:13:44 EST 2003
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
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
See how closely (or not!) your predictions match the actual data.
1. build the Java client.
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:
Name your client:
- 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.
- Send the username and password from the client to the server,
for validation, using either the LOGIN or the NEW command.
- Wait until the server responds, with either an OKAY or a BAD command.
- 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
When creating a new user, the server will respond with an OKAY
- 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.
- Send a request to the server for game data and wait to receive it.
- After receiving the game data from the server, parse the game data
and use it to initialize the game.
- 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.
- When the game is over, send the game results to the server.
- 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.
so for example, mine is called
2. modify the server.
The server should already do the following (from hw#3):
Now you need to modify the server to handle the game commands that your
client is now sending.
This means that the server must:
- 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
- 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
Then the server can exit.
- 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:
so for example, mine is called
3. write a makefile.
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:
so for example, mine is called
You need to submit your program in two ways: electronically and hard copy.
Follow the submission instructions.
(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).