CMPUT379, Assignment 2, WINTER 2001
Due Wednesday, March 14, 2001
Key concepts: Processes, inter-process communication, client-server
model, network programming, sockets, managing asynchronous i/o
The problem
You are to write a server process to act as a dealer for the card game,
Black
Jack. For the purposes of this assignment we will be using a simplified
version of the rules for the game (see
blackjack rules
provided). These rules are to be realized in the server (and dealer) with
a specific protocol (see below) which will allow ANY client process
that also implements the protocol to connect to your server and play the
game.
Criteria that your server should satisfy include:
-
The server should be accessible from both local and remote sites. This
means that the dealer and the player processes may reside on different
machines, provided that these machines are connected by the Internet.
-
Use of virtual circuit communication channels in the form of internet
domain sockets (domain type: AF_INET, socket type: SOCK_STREAM---these
concepts will be covered in more detail in the lab).
-
The master socket to which clients will connect should use port number
2000 + the last three digits of your student ID. For example, if your student
ID is 223456, you should be using port number 2456. This port number should
appear in the header of the document that will accompany your solution.
-
Busy-waiting must be avoided.
-
The server must support the protocol outlined below.
The protocol
The game is played in ``rounds''. At the start of each round a number of
player connections are accepted by the server, and a hand of blackjack
played with each in accordance with the rules provided.
The server initiates a round by accepting the first available connection
from its master socket. Player processes connect to the server to express
their willingness to participate in the current round.
When a player first connects, it sends the server the magic word
0xdeadbeef.
This word indicates to the server that this is an actual player that knows
the rules of the game (and uses the correct protocol). A player that doesn't
send this word within 5 seconds should be disconnected (its socket is closed)
and ignored. Once the first player is authenticated, the server should
proceed to accept additional players in the same fashion until:
-
a total of 4 players have been accepted (initial + 3 more) OR
-
30 seconds have elapsed since the first player requested a game,
at which point the ``table is closed''. The round then commences
with the available player connections.
Once a set of players have been accepted as outlined above, the server
should fork off a child process to execute the dealer process that will
play the game with the connected players. The server should wait for the
dealer to finish the round (child process terminates), at which point it
should begin collecting players for a new round, and the process repeats
itself.
Note that the master socket is never closed.
The dealer begins by sending each player their cards, followed by the
single visible dealer card (see format below). Note that the dealer
will be have up to 4 hands of its own (one for each player it is playing
against). As soon as a player receives this information, it may begin
playing its hand in accordance with the rules. Specifically, a player may
send the message "HIT", to which the dealer will respond with another card
(same format). Alternatively the player may send the message "STAND"
to indicate that they are satisfied with their hand. If a "hit" results
in the player going bust, the dealer disconnects the appropriate player
connection after "dealing" (sending) the card---the dealer automatically
wins that hand. Note that the dealer is aware of this since it can
keep track of the player's hand,
Important: The dealer must process players' requests as soon
as they are received; the dealer will have to monitor all connections for
data simultaneously, rather than process them in round robin fashion.
Once all players have either chosen to stand or gone bust, the dealer
plays the hand it is holding for each remaining connected player according
to the criteria specified in the rules. Each card
the dealer draws is sent to the player in turn (as noted above). Once the
dealer either stands or goes bust, the respective player connection is
terminated and the dealer moves on to the next player (if any). Note:
since the player knows all cards played, it is capable of determining the
outcome of the game for itself.
Once the dealer has completed a hand with all players, or all connections
have been terminated, it exits tt round is finished.
Players keep track of their own bets and do their own book-keeping with
regard to wins and losses.
Format of cards and messages: Cards are represented by
a sequence of two char's. The first
char is the ASCII
value of the card: { 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A } (note that
we define "10" to be "T" to allow every card to be represented by a single
character), the second is the ASCII value of the suit: { S, H, D, C } (for
example, player's cards: 7 of Hearts, 3 of Clubs, together with the dealer's
visible card the Ace of Diamonds ,would appear as the stream of characters:
7H3CAD.
Messages are passed as ASCII text, terminated by a null byte
('\0').
Caveat
The dealer should operate correctly in the face of possibly misbehaving
players. In particular, if a player terminates unexpectedly or sends a
message that doesn't conform to protocol rules, it should be disconnected.
You may assume that a player is terminating if it doesn't respond with
its "STAND" or "HIT" message within 5 seconds (except after the player
"STAND"s of course). This 5 second limit should be applied to each connected
player individually.
Example
An example of an exchange between a player and the server/dealer for a
single hand (from the player's perspective) might go something like this:
-
connect to server - send "0xdeadbeef"
-
receive: 7H4DAH
my hand: 7-hearts & 4-diamonds (value of hand = 11, dealer has
Ace-hearts showing)
-
send: "HIT"
-
receive: 9S
now have: 7-hearts, 4-diamonds, 9-spades (value of hand = 20)
-
send: "STAND"
-
receive: 3C4H, further reads indicate EOF (i.e. connection was
terminated)
dealer had A-hearts, 3-clubs, 4-hearts, and (as per the rules) stood
on 18 - I win!!!
Deliverables
-
On-line files
Before the due date (March 14, 2001) you should create a directory called
assig2 in your cvs directory. Add to this directory the following files:
-
server.c containing the source code of the server process
-
dealer.c containing the source code of the dealer process
-
client.c containing the source code of at least one example
client
-
[mM]akefile to create the executable versions for server, dealer
and client. Your makefile should include a 'clean' target
-
report.* containing the electronic version of the report for this
assignment. You are free to choose from four fileformats for the report
(* stands for whatever extension you want to add), that is, .html, .pdf,
.ps or .txt. In the latter case, .txt files must not have more than
76 characters per line.
-
Documentation
A hardcopy report must be handed in on Wednesday, March 14 (in class).
The report should be spell-checked and should not be more than 4 pages
(to this add the required cover sheet, see below).
Your documentation should include the following (of necessity you will
have to be brief, but concise) :
-
Statement of the problem solved.
-
Design
State any important or relevant design decisions you made in
implementing your server and dealer processes.
-
Decisions/assumptions/justification
State any assumptions you may have made in solving the problem and
known limitations of your implementation. Justify the design decisions
you made in the previous section.
-
Testing
Indicate what methods you have used to ensure the correct/ appropriate
operation of your solution.
You are also required to put a cover sheet on your documentation which
contains the following information:
-
Student Name and student ID
-
Unix ID
-
Class Section and Instructor's name
-
Lab Section and TAs name
Copyright © Department of Computing
Science.
All rights reserved.
Last update:Feb 10 08:24:32 2001