threadedscoreserver.java

来自「java3D game engine design of the source 」· Java 代码 · 共 59 行

JAVA
59
字号

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

/* A threaded server that stores a client's score (and name) in a
   list of top-10 high scores.

   The list is maintained in a file SCORFN, and loaded when the
   server starts.
   The server is terminated with a ctrl-C

   The server processes a client by creating a 
   ThreadedScoreHandler object.

   Derived from ThreadedEchoServer.java (Listing 3-5, p.161) in
      Core Java 2, Volume II -- Advanced Features
      Horstmann and Cornell
      Sun Microsystems Press, 2000, 4th Edition
*/

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


public class ThreadedScoreServer
{
  private static final int PORT = 1234;
  private HighScores hs;


  public ThreadedScoreServer()
  // Concurrently process clients forever
  {
    hs = new HighScores();
    try {
      ServerSocket serverSock = new ServerSocket(PORT);
      Socket clientSock;
      String cliAddr;

      while (true) {
        System.out.println("Waiting for a client...");
        clientSock = serverSock.accept();
        cliAddr = clientSock.getInetAddress().getHostAddress();
        new ThreadedScoreHandler(clientSock, cliAddr, hs).start();
      }
    }
    catch(Exception e)
    {  System.out.println(e);  }
  }  // end of ThreadedScoreServer()


  // -----------------------------------

  public static void main(String args[]) 
  {  new ThreadedScoreServer();  }

} // end of ThreadedScoreServer class

⌨️ 快捷键说明

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