⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 318-322.html

📁 java game programming e-book
💻 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,&nbsp;Macmillan Computer Publishing
    <br>
    <b>ISBN:</b>&nbsp;1571690433<b>&nbsp;&nbsp;&nbsp;Pub Date:</b>&nbsp;11/01/96</font>&nbsp;&nbsp;
</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=318-322//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="315-318.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="322-326.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Types of Multiplayer Games: The Programmer&#146;s Perspective</FONT></H4>
<P>From a programming perspective, there are two ways of handling a multiplayer game: local and remote.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Local Games</FONT></H4>
<P>A local game is the more common type. This is the kind of game where all of the players are at the same computer, and they take turns entering their moves. The advantage to writing this type of game is that it is easy to program. The advantage to players is that it requires only one machine. It also has several disadvantages for players. Usually, game play is slow, since each player must wait for all of the other players to complete their moves before he or she moves again. This can be especially aggravating if there are more than a few players. This type of game also presents difficulties for programmers. Especially in games that require each player to keep information secret from other players (like poker, for instance), the fact that all users share a common terminal is disadvantageous.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Remote Games</FONT></H4>
<P>A remote, or <I>networked</I>, game allows multiple players on different computers to compete across some kind of link (often a modem or network). Some programs allow players to play action-type games simultaneously, while others allow them to take turns at strategy games while &#147;chatting&#148; in between moves. This type of game has many advantages, mainly that it allows physically distant players to interact. It also avoids the problem of secret information that local games suffer from. Unfortunately, networked games also present their own set of difficulties from a programming point of view. First of all, they have traditionally been more difficult to write than local games. Another problem is that of speed. Especially for real-time games, each player needs constant updates about the position and status of other players. If this information is too slow in arriving, it makes the game unplayable. Luckily, networks of today have reached the point where sufficient data transfer rates can be attained for even the most rapidly paced games.</P>
<H4 ALIGN="LEFT"><A NAME="Heading10"></A><FONT COLOR="#000077">Choosing the Proper Networking Architecture</FONT></H4>
<P>Java makes it easy to write remote multiplayer games because it makes networking so simple. The same skills we used in Chapter 8 to write a high score server can easily be applied to writing a multiplayer game. In Chapter 8, we learned how to implement a client-server networking architecture, but it is important to note that this is not the only architecture suitable for multiplayer games. One method commonly used is called <I>peer-to-peer</I> networking.</P>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Peer-to-Peer Networking</FONT></H4>
<P>Peer-to-peer networking entails having two or more clients connected directly. There is no need for a server (hence each client is said to be a &#147;peer&#148; of the other clients), and clients can choose which other clients will receive information. A good example of peer-to-peer networking that you have probably already used is a modem. When you connect to another computer via a modem, no server is required, and neither client has priority. While this sort of networking scheme works well for two connected clients, it becomes unwieldy when used with more. Think of the complications required for 10 clients connected to each other: the total number of sockets required to make these connections is 10!, or 3,628,800 connections&#151;quite an unacceptable number for only 10 connected clients. Luckily, there are other methods of doing peer-to-peer networking that require fewer connections. One such method is a <I>ring</I>.</P>
<H4 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Networking Rings</FONT></H4>
<P>In a ring networking structure, each client is connected to two others. By linking each client to the next, network rings allow a message to be sent to many more computers than are connected to any one client. When a client receives a message from one of the two other clients it is connected to, it passes it on to the other client. This type of setup is often used in local area networks, where connections between computers are physical links. However, on the Internet, links between computers take the form of Sockets, which are abstract (and therefore cheaper than physical links). The Internet is largely unsuitable for this type of structure, because if one computer becomes disconnected (which happens often in the dynamic, chaotic world of the Internet), the whole ring suffers. Therefore, this is not an optimal choice for a Java applet running on the Internet.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Peer-to-Peer Networking in Java</FONT></H4>
<P>Even though peer-to-peer networking may seem attractive for some multiplayer games, most current implementations of Java have special security restrictions that prohibit any applet from communicating with any machine other than the one it was loaded from. There are many practical reasons for this security precaution, but it makes true peer-to-peer networking in Java almost impossible. Therefore, only one choice remains as a viable networking structure for multiplayer games.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Adapting Client-Server Architecture to Multiplayer Games</FONT></H4>
<P>To get around this security limitation, we must still use client-server networking even for games of only two players. This approach is in many ways superior to the peer-to-peer approach, because client-server networking allows for many players interacting simultaneously. The program we are going to write in this chapter implements this kind of networking relationship. One server will interact with an infinite number of clients <I>at the same time</I>.</P>
<P>This is a key difference between this program and the one we wrote in Chapter 8. The high score server only handled one connection at a time. Other connections had to wait in line until the server was finished with the current connection before they could be serviced. This time, the server is going to keep and maintain a list of all of the currently connected clients and interact with all of them. The difference between these two is illustrated in Figures 9-1 and 9-2.</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/09-01.jpg',561,383 )"><IMG SRC="images/09-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/09-01.jpg',561,383)"><FONT COLOR="#000077"><B>Figure 9-1</B></FONT></A>&nbsp;&nbsp;Diagram of clients waiting in line for interaction with server</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/09-02.jpg',563,383 )"><IMG SRC="images/09-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/09-02.jpg',563,383)"><FONT COLOR="#000077"><B>Figure 9-2</B></FONT></A>&nbsp;&nbsp;Diagram of server with multiple clients<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="315-318.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="322-326.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -