📄 759-762.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:The Internet MahJong Server</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></script><SCRIPT><!--function popUp(url) { var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) { /* get the query value */ var i = escape(fm.query.value); if (i == "") { alert('Please enter a search word or phrase'); return false; } /* query is blank, dont run the .jsp file */ else return true; /* execute the .jsp file */}//--></script></HEAD><BODY>
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
<font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
<br>
<font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
<br>
Sams, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</td>
</tr>
</table>
<P>
<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=18//-->
<!--PAGES=759-762//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="756-759.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch19/763-771.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>To make the database easy to access, I also expanded the protocol and the client GUI a little, to let you “finger” other players. This brings up a window to show the other player’s personal information as he put it on the server.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading39"></A><FONT COLOR="#000077">Dealing with Bad Connections</FONT></H4>
<P>The Internet is not perfect. Many a time the network connection between a player’s client and the server breaks down. In this situation, it’s advisable to free up that player’s slot. Unfortunately, the current Java implementations make this task harder than it ought to be. Instead of immediately throwing an IOException on dead sockets, Java may simply block forever on read/write calls.
</P>
<P>My solution is using <I>ping</I> <I>packets</I>. On the client side, we spawn a thread that sends a ping packet once every 30 seconds. The ping packet carries no information other than that it has reached the server. On the server side, every time a player’s listener thread reads in a ping packet, it increases a ping counter. We use one extra server thread, too, called the WatchDog, which is usually sleeping, but wakes up once every minute or so to check the counters of all players. A counter’s being 0 means that player’s network connection is hosed, and we disconnect him. Otherwise, we reset his counter to 0 for the next round.</P>
<H4 ALIGN="LEFT"><A NAME="Heading40"></A><FONT COLOR="#000077">Adding Timeout Functionality</FONT></H4>
<P>The ping method has the added bonus of being able to gauge a user’s idle time. If a player is playing on a table, and the ping counter reaches 2 before we receive any CP_PLAY packet from him, we can make a safe guess that he is probably answering the phone or busted by his boss; in either case we can kick him off the table to let someone else play.
</P>
<P>An Urge! feature comes with the timeouts. There is an Urge! button that, when clicked on, will ask the server to alert everyone on the table to pay more attention to the gare. The alert comes in the form of a “Duke” figure waving at you, accompanied by a sound (which can be turned off). A timer on the server side makes sure this feature isn’t abused.</P>
<H4 ALIGN="LEFT"><A NAME="Heading41"></A><FONT COLOR="#000077">Debugging the Game</FONT></H4>
<P>After implementing everything up to this point, we are looking at tens of thousands of lines of source code. Debugging is tough even if you have the best development tools at your disposal. It is therefore important to divide the code into functional chunks, write one at a time, then fully debug and gradually add onto it. For example, I finished the basic client-server pair before adding MahJong features, and then used subsets of MahJong rules before handling the complete game.
</P>
<P>Another way to speed up debugging is to write some extra code to get you started in a midgame situation. I created a back-door on the server, such that I could send to the server a specially formatted message string that encodes a particular hand I want to have, and the server will give me that hand. This way I can test game play without betting my luck on drawing the tiles I want to test on.</P>
<H3><A NAME="Heading42"></A><FONT COLOR="#000077">Comments</FONT></H3>
<P>There are several sites running public servers with my software. You can access them from my Internet MahJong Meta-Server Web page at URL <A HREF="http://www.math.princeton. edu/∼ztfeng/mj_servers.html">http://www.math.princeton. edu/∼ztfeng/mj_servers.html</A>. You can also find there more information about MahJong, documentation of server features, and the most recent source code release.</P>
<P>The companion CD-ROM of this book also contains the source code. However, it has not been polished as I’ve done to the code segments given in this chapter; indeed, some code that achieves more or less the same functionality looks much uglier there! Look for future releases, which will contain more features, bug fixes, and which may or may not be more readable.</P>
<P>To summarize, Java’s platform independence, built-in networking, multithreading, and the still-immature but promising AWT library make developing multiplayer Internet games like the MahJong Server quite easy. Indeed, it took me less than three weeks to get the first version running. If I had started this project in C and Motif or Win32, I would have eventually given up as I had done with several previous endeavors. Let’s wish Java well-being, and enjoy programming in it!</P>
<H3><A NAME="Heading43"></A><FONT COLOR="#000077">Suggestion Box</FONT></H3>
<DL>
<DD><B>•</B> When writing a multiplayer game, first get a simple client and sever pair working, and gradually add to the client-server protocol to do fancy things.
<DD><B>•</B> Use multiple threads to keep track of different players, but handle synchronization with care.
<DD><B>•</B> For a game with complicated rules, it’s often a good idea to express it as a finite-state machine, as I have done with MahJong. The game is in one of several states, and valid game play moves the game from one state to another.
<DD><B>•</B> If your game needs an extensible player database, you may want to look at the RecordFile class in the IMS source.code. You can read/write variable-length records with it. For simple score-keeping, the RandomAccessFile class in java.io probably suffices.
<DD><B>•</B> The easiest way to write a multiplayer game is probably to study somebody else’s code and replace appropriate chunks with your own. If you start your own game from the latest version of IMS, then you will already have a lot of raw functionality!
</DL>
<H3><A NAME="Heading44"></A><FONT COLOR="#000077">Summary</FONT></H3>
<P>This chapter covered the design and implementation of a multiplayer Internet game. We studied the client-server architecture, the protocol, socket programming, the use of threads, and how to implement a complicated game like MahJong. You also saw plenty of examples of AWT in action. Now you are ready to bring games to the Internet, where Java is intended to rule!
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="756-759.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="../ch19/763-771.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -