[ Pobierz całość w formacie PDF ]
.At the heart of these solutions is the idea of having more thanone server for your multiuser environment.Such a configurationmay involve one central server that has the power to spawn otherservers (on other ports), or a central server that acts as a "gatekeeper,"accepting clients only if there are openings (see Figure 27.2).Figure 27.2 : An example of a gatekeeper system.Either case requires the development of a richer protocol, butalso provides you with opportunities for greater power over yourenvironment.For example, you can establish several two-playergames managed by a central server.By designing the applet toconnect on a given port (1626,for example) and to request entrance to a game, the server onthat port can act as a manager by beginning a new game on anotherport (1627, for example)and informing the applet of the new port number.The applet canthen disconnect from port 1626,connect to port 1627, andwait for the central server to send another user to the same port.Once the central server has sent two players to port 1627,it starts a new server on the next available port (1628,for example) and can continue in this manner indefinitely (seeFigure 27.3).Figure 27.3 : An example of a central server/children server system.In the cases of the central server/children server system andthe gatekeeper system, the server can assign each client an identificationnumber at connection time.This arrangement not only allows theserver to keep track of who is sending the information, it alsoenables the server to send user-specific messages (for example,Player One: You can't do that).Both of these tasks can be facilitated by appending a simple identificationletter or number to each command in the protocol.(B112could mean, for example, that player 1just bought plot 12.)SummaryEven though this chapter did not deal with many topics, keep inmind that the power of Java is its abstract capability to facilitatea multiuser environment, not its specific lexical constructs.Here are a few issues relating to the Java language that you shouldkeep in mind when developing a multiuser environment:Develop a user-friendly graphical interface.By using the java.awt.Eventclass, you can respond to the user's interactions in an effectivemanner.Create a client class that extends thejava.lang.Thread class.Although you can create as many methods as you want within thisclass, the best approach is to harness the run()method's capacity to run concurrently to develop an effectivemeans of reading data from the stream.Also, remember to use someform of flag that provides you with a way of controlling the lifetimeof the run() method.Develop the framework by which the appletand client classes will be able to communicate.This is best doneby making an instance of the client class a field in the appletclass.By making the appropriate methods of the client class public,your applet class can perform such tasks as connecting, disconnecting,and sending data.Create a "translating" methodin your applet class.Doing so enables the client class to sendthe parsed data to the applet class to elicit the proper response.Contactreference@developer.com with questions or comments.Copyright 1998EarthWeb Inc., All rights reserved.PLEASE READ THE.Copyright 1998 Macmillan Computer Publishing.All rights reserved
[ Pobierz całość w formacie PDF ]