📄 273-281.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:Implementing a High Score Server on a Network</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=8//-->
<!--PAGES=273-281//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch07/267-272.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="282-284.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H2 ALIGN="CENTER"><FONT COLOR="#000077"><I>Part II<BR>Advanced Game and Graphics Techniques
</I></FONT></H2>
<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Chapter 8<BR>Implementing a High Score Server on a Network
</FONT></H2>
<P><I>Eric Ries</I></P>
<P><FONT SIZE="+1"><B>Goals:</B></FONT></P>
<P>Understand client-server networking fundamentals
</P>
<P>Implement high scores in Java</P>
<P>Use Threads, Sockets, and Files</P>
<P>Build a server</P>
<P>Allowing competition among players enhances the enjoyment of any game. In traditional programming environments, a “high score list” is used to allow players to keep track of their best scores, thus providing an incentive for further play. Java extends this paradigm to a new level. By allowing communications over the Internet, Java allows players to compete against other players worldwide.</P>
<P>Implementing a high score server in Java is relatively simple when compared with older-generation languages. To do this, you need two separate components: the client and the server. The client is the program (your game, in this case) that runs on the user’s computer. The server is the program that runs on the machine where your programs were initially located. By obtaining information from, and reporting back to, the server, your Java game can display and continually update a list of the best players of your game. This can be a decisive advantage for your game over other games that compete for users’ attention.</P>
<P>In this chapter, there are two things we need to discuss. The first is using Java to handle high scores using concepts this book has already discussed. The second part of the chapter discusses using Java to implement these concepts over a network.</P>
<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Why Use Java for Network Programming?</FONT></H3>
<P>Client-server communication over the Internet has obviously been around much longer than Java. Java, however, brings with it an unprecedented level of ease-of-use in network programming. Being game programmers, we have absolutely no need to waste our time with all of the details of Internet communications (and there are <I>many</I> details). Java allows us to focus on the more important aspects of the program while it transparently takes care of the messy stuff in the background.</P>
<H3><A NAME="Heading3"></A><FONT COLOR="#000077">What Is Client-Server Networking?</FONT></H3>
<P>Most individuals with a reasonable amount of computer experience understand the basics of client-server networking. However, generations of computer science majors have managed to come up with an entire lexicon designed to confuse you. Things like sockets, ports, packets, and streams may sound like they have more to do with fishing than with computers, so let’s start with a metaphor to help us along.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading4"></A><FONT COLOR="#000077">Basic Client-Server Terminology</FONT></H4>
<P>Pretend you are trying to reach customer support at a huge corporation (a painful experience all of us have had). You call the company and reach the receptionist, who asks you for an extension. Luckily, you have the number handy, and you are transferred to the customer representative. The two of you have a delightful conversation, and then you both hang up. The whole process is simple and straightforward; any child could tell you how it’s done. Unfortunately, to do something simple like this on a network requires a whole new vocabulary. Let’s start with the most common terms we need to know:
</P>
<DL>
<DD><B>•</B> <I>Client</I>. The entity making the call (in our example, you).
<DD><B>•</B> <I>Server</I>. The entity processing your requests (the company, in our example).
<DD><B>•</B> <I>Socket</I>. Computers on the Internet communicate just like you did with your customer service representative. However, instead of telephones, computers use <I>sockets</I>. Java provides you with a very handy Socket class, which handles all of the low-level code for network communications. All you have to do is dial.
<DD><B>•</B> <I>IP address</I>. For one computer to call another computer, it needs a “phone number.” In Internet language this is called an IP (for <I>Internet protocol</I>) address. This is a series of numbers and periods that looks something like this: 131.247.1.58. While this may not be too meaningful to a human being, an Internet computer can use it just like a phone number.
<DD><B>•</B> <I>Domain name server (DNS)</I>. What if you didn’t know the number of a company? For a computer, this is never a problem, because computer memory is flawless. Humans are not so well equipped, so we sometimes rely on a phone book to find the number we’re looking for. On the Internet, this is called a <I>domain name server (DNS)</I>, and it is what allows you to type in an address like “<A HREF="www.waite.com">www.waite.com</A>” instead of all those pesky numbers. Using an IP address or its DNS equivalent, a client program can open a socket connection to a server. Bear in mind that <I>every</I> computer connected to the Internet must have a <I>unique</I> IP address assigned to it.
<DD><B>•</B> <I>Port</I>. What does a client do once it has connected to a server? Just as in our example, it gets the receptionist, who asks it for an extension. In Internet jargon, the extension is called the <I>port</I>. On any one machine, any program can access any port, which is usually given a number between 1 and 9999.
<DD><B>•</B> <I>Service</I>. No two programs can share a port, so each port represents a different <I>service</I> offered by the server. In order for a client and a server to communicate, the server must be listening to the same port that the client is calling on. Otherwise, your client might get sales instead of customer support.
<DD><B>•</B> <I>Protocol</I>. Now, when you finally get through to someone on their extension, it doesn’t do anybody any good if they speak Korean and you speak Portuguese. In order to do any kind of useful communicating, the client and the server must use the same <I>protocol</I>. A protocol is like a language that computers use to speak to each other. A protocol defines the order and type of interactions that can take place in a socket connection. Even though you may not know it, you are probably familiar with many protocols already.
<DD><B>•</B> <I>HyperText Transfer Protocol (HTTP)</I>. This is the most popular protocol on the World Wide Web. It is used to send a wide variety of textual and multimedia data. Other common ones include Gopher, Telnet, FTP , WAIS, and SMNP. The protocols that we will be using are far less complex, but the concepts are the same.
</DL>
<P>A typical phone conversation is shown in Figure 8-1, and its networking equivalent is shown in Figure 8-2.
</P>
<P><A NAME="Fig1"></A><A HREF="javascript:displayWindow('images/08-01.jpg',468,584 )"><IMG SRC="images/08-01t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/08-01.jpg',468,584)"><FONT COLOR="#000077"><B>Figure 8-1</B></FONT></A> Diagram of telephone conversation</P>
<P><A NAME="Fig2"></A><A HREF="javascript:displayWindow('images/08-02.jpg',466,585 )"><IMG SRC="images/08-02t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/08-02.jpg',466,585)"><FONT COLOR="#000077"><B>Figure 8-2</B></FONT></A> Networking equivalents of telephone metaphor<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="../ch07/267-272.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="282-284.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -