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

📄 threadedscorehandler.java

📁 java3D game engine design of the source [three-dimensionalvirtualrealitynetworkprogram] - "virtual
💻 JAVA
字号:

// ThreadedScoreHandler.java
// Andrew Davison, March 2002, dandrew@ratree.psu.ac.th

/* A threaded handler, called by ThreadedScoreServer to process 
   a client.

   Understood input messages:
	get						-- returns the high score list
    score name & score &	-- add the score for name
	bye						-- terminates the client link
*/

import java.io.*;
import java.net.*;


public class ThreadedScoreHandler extends Thread
{
  private Socket clientSock;
  private String cliAddr;
  private HighScores hs;

  public ThreadedScoreHandler(Socket s, String cliAddr, HighScores hs)
  {
    clientSock = s;  
    this.cliAddr = cliAddr;
    this.hs = hs;
    System.out.println("Client connection from " + cliAddr);
  }


  public void run()
  {
    try {
      // Get I/O streams from the socket
      BufferedReader in  = new BufferedReader( 
	  		new InputStreamReader( clientSock.getInputStream() ) );
      PrintWriter out = 
			new PrintWriter( clientSock.getOutputStream(), true );  // autoflush

      // interact with a client
      processClient(in, out);
 
      // Close client connection
      clientSock.close();
      System.out.println("Client (" + cliAddr + ") connection closed\n");
    }
    catch(Exception e)
    {  System.out.println(e);  }
  }  // end of run()


   private void processClient(BufferedReader in, PrintWriter out)
   // Stop when the input stream closes (is null) or "bye" is sent
   // Otherwise pass the input to doRequest()
   {
     String line;
     boolean done = false;
     try {
       while (!done) {
         if((line = in.readLine()) == null)
           done = true;
         else {
           System.out.println("Client msg: " + line);
           if (line.trim().equals("bye"))
             done = true;
           else 
             doRequest(line, out);
         }
       }
     }
     catch(IOException e)
     {  System.out.println(e);  }
   }  // end of processClient()


  private void doRequest(String line, PrintWriter out)
  /*  The input line can be one of:
             "score name & score &"
      or     "get"
  */
  {
    if (line.trim().toLowerCase().equals("get")) {
      System.out.println("Processing 'get'");
      out.println( hs.toString() );
    }
    else if ((line.length() >= 6) &&     // "score "
        (line.substring(0,5).toLowerCase().equals("score"))) {
      System.out.println("Processing 'score'");
      hs.addScore( line.substring(5) );    // cut the score keyword
    }
    else
      System.out.println("Ignoring input line");
  }  // end of doRequest()


}  // end of ThreadedScoreHandler class

⌨️ 快捷键说明

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