FileDocCategorySizeDatePackage
HelloThread.javaAPI DocExample3993Sat Sep 12 03:01:00 BST 1998borland.samples.apps.chess.server

HelloThread

public class HelloThread extends Thread

Fields Summary
ServerSocket
serverSocket
Constructors Summary
HelloThread()


   
    ChessServer.availablePort = new boolean[ChessServer.maxConnections];
    for (int i=0;i<ChessServer.maxConnections;i++)
      ChessServer.availablePort[i] = true ;
    //System.out.println("Hello");
    try {
      serverSocket = new ServerSocket(ChessServer.basePort - 1);
      System.out.println("Hello1");
    }
    catch (IOException e) {
      System.out.println("Could not listen on port: " + 4444 + ", " + e);
      System.exit(1);
    }
  
Methods Summary
protected voidfinalize()

    if (serverSocket != null) {
      try {
        serverSocket.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      serverSocket = null;
    }
  
public voidrun()

    //System.out.println("HelloThread: running");
    if (serverSocket == null)
      return;
    try {
      System.out.println("Address of the Server :" + InetAddress.getLocalHost().toString()) ;
    }
    catch (Exception e) {
      System.out.println("HelloThread: " + e);
      System.exit(1);
    }
    while (true) {
      System.out.println("HelloThread: Waiting for someone to talk to me: " );
      Socket clientSocket = null;
      try {
        clientSocket = serverSocket.accept();
      }
      catch (IOException e) {
        System.out.println("Accept failed: " + e);
        System.exit(1);
      }

      try {
        System.out.println("HelloThread: set up data streams");

        DataInputStream is = new DataInputStream(
                             new BufferedInputStream(clientSocket.getInputStream()));
        PrintWriter os = new PrintWriter(clientSocket.getOutputStream());
            //             new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
        InputStreamReader isr  = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String inputLine, outputLine;
        String name;
        //System.out.println("HelloThread: Someone to talk to! " );

        if ((inputLine = br.readLine()) != null) {
          System.out.println("Hello Thread: Received:" + inputLine);
          outputLine = "";
          //someone wants to talk ; tell them the number of their
          //private port;
          if (inputLine.startsWith("Hello")) {
            int portNumber = ChessServer.getPortNumber();
            new SendThread(portNumber).start();
            outputLine = String.valueOf(portNumber);
            System.out.println("Assign " + clientSocket.getInetAddress().toString() + " to port " + outputLine) ;

          }
          os.println(outputLine);
          os.flush();
        }
        os.close();
        is.close();
        clientSocket.close();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
      catch (Exception e) {
        System.out.println("HelloThread:" + e);
      }
    }