Java Reference
In-Depth Information
Note
FormoreinformationonJDBC,checkoutTheJavaTutorial's“JDBC:Data-
base Access” trail at
http://download.oracle.com/javase/tutorial/
EXERCISES
ThefollowingexercisesaredesignedtotestyourunderstandingofnetworkAPIsand
JDBC:
1.CreateanetworkedversionofBlackjack—theversionofthisgametoimplement
isdescribedafterthisexercise.Implementa
BJDealer
applicationthatserves
asthedealeranda
BJPlayer
applicationthatservesastheplayer.
BJDealer
waits for a player connection indication and then creates a background thread
to service the player—this makes it possible for the dealer to play independent
gameswithmultipleplayers.When
BJDealer
acceptsasocketfromaplayer,
it creates
java.io.ObjectInputStream
and
java.io.ObjectOutputStream
objectsforcommunicatingwiththeplay-
er. Similarly, when
BJPlayer
creates a socket for communicating with the
dealer, it creates
ObjectInputStream
and
ObjectOutputStream
ob-
jects. Because the
ObjectInputStream(InputStream in)
constructor
blocks until the corresponding
ObjectOutputStream
instance has written
and flushed a serialization stream header, have each of
BJDealer
and
BJPlayer
immediately call the
flush()
method on the created
Ob-
jectOutputStream
instance.
BJDealer
serializes
Card
objects and
String
-basedstatusmessagesto
BJPlayer
;
BJPlayer
serializes
Card
ob-
jectsand
String
-basedcommandsto
BJDealer
.
BJDealer
doesn'tpresent