📄 322-326.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:Advanced Networking and Multiplayer Gaming Concepts</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=9//-->
<!--PAGES=322-326//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="318-322.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="326-328.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
</P>
<H3><A NAME="Heading15"></A><FONT COLOR="#000077">Understanding the Chat Room Server</FONT></H3>
<P>In Chapter 8, we talked first about the client applet. In this chapter, we are going to talk about the <I>server</I> side of this applet first. This is because the ChatServer will have many more responsibilities than the client-driven HighScoreServer.</P>
<H4 ALIGN="LEFT"><A NAME="Heading16"></A><FONT COLOR="#000077">Functions of a Chat Room Server</FONT></H4>
<P>Let’s talk about what the ChatServer has to do:
</P>
<DL>
<DD><B>•</B> <I>Connect to multiple clients simultaneously.</I> In order to have a chat “room,” the server must be able to connect to and interact with multiple clients. We will accomplish this by having the server class create special Thread objects to deal with each connection.
<DD><B>•</B> <I>Receive and process commands from each client.</I> When the client sends a command to the server, the server must take the specified action by sending a command to one or more additional clients. We will have several types of messages the server and client can send and receive.
<DD><B>•</B> <I>Keep track of the alias used by each client.</I> This is the name that the user provides when he or she logs in. The server must be able to generate a list of all current aliases if asked.
<DD><B>•</B> <I>Maintain a list of current connections.</I> It must also periodically remove any disconnected clients from this list.
</DL>
<P>How can we go about writing a program that does all of this? Clearly, we cannot write it all into one application. Luckily, we are using an object-oriented language that will simplify our task greatly. We will create a hierarchy of classes, each with a specific task, and then use them together. Figure 9-3 shows what it will look like. The diagram illustrates how a Socket connection comes in from a client on the Internet and is passed down through the hierarchy until it “plugs in” to a Thread that manages it.
</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/09-03.jpg',562,382 )"><IMG SRC="images/09-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/09-03.jpg',562,382)"><FONT COLOR="#000077"><B>Figure 9-3</B></FONT></A> Hierarchy of ChatServer classes</P>
<H4 ALIGN="LEFT"><A NAME="Heading17"></A><FONT COLOR="#000077">ChatServer Classes</FONT></H4>
<P>What are all of these classes? Let’s talk about them a little more in-depth:
</P>
<DL>
<DD><B>•</B> <I>ChatServer</I> is simply the main application class. It will do nothing except create a new ChatServerThread, which will actually do the work.
<DD><B>•</B> <I>ChatServerThread</I> is a Thread that will wait for a connection. When it receives one, it will pass it along to sClientGroup.
<DD><B>•</B> <I>sClientGroup</I> does most of the work. It will maintain a <I>Vector</I> of all of the current connections. A Vector is a class provided by Java for grouping things similar to an array, but it is dynamic. More on this later. Whenever sClientGroup receives a new connection for ChatServerThread, it will create a new sClientThread to handle the connection, and put this Thread in the Vector list. In addition, sClientGroup is a Thread that periodically checks every connection in the Vector to make sure it is still active. The class also has a function that handles incoming messages from the clients.
<DD><B>•</B> <I>sClientThread</I> is a Thread that waits for some input from the Client to which it is assigned. Whenever it gets input, it passes it back to its <I>parent</I>, sClientGroup. It also allows sClientGroup to give it a message that it will dutifully pass along to its client. Each sClientThread can handle only one client, and it is up to sClientGroup to assign a client to it when it is created.
</DL>
<P>This may sound extremely complicated, but it’s really not. Pretend you are an important business executive. You have many, many important tasks to complete, far more than you can accomplish on your own. What do you do? You delegate. You take a whole group of your subordinates, and give each one a task. As your boss gives you new tasks, you pass them along to new subordinates. As tasks get completed, you remove them from your to-do list. Most importantly, when subordinates need to talk to each other, they contact you and you send the appropriate memos. This is exactly how the sClientGroup works. The ChatServerThread is your boss, and each sClientThread is a subordinate.
</P>
<P>With this metaphor in mind, let’s jump straight into the code!</P>
<H3><A NAME="Heading18"></A><FONT COLOR="#000077">Implementing the Chat Room Server</FONT></H3>
<P>We are now ready to start writing code. Initially, we must create several classes.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Creating the ChatServer Class</FONT></H4>
<P>This is the easiest step of all. This class file will simply create a new ChatServerThread and run it. It won’t compile, unfortunately, because we haven’t written the ChatServerThread program! Create a file called ChatServer.java and put the following into it:
</P>
<!-- CODE //-->
<PRE>
import ChatServerThread;
class ChatServer {
public static void main(String args[]) {
new ChatServerThread().start();
}
}
</PRE>
<!-- END CODE //-->
<P>Save this file and then forget about it for a while.
</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="318-322.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="326-328.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -