home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / KnockKnockServer.java < prev    next >
Text File  |  1997-05-20  |  2KB  |  68 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.net.*;
  18. import java.io.*;
  19.  
  20. class KnockKnockServer {
  21.     public static void main(String[] args) {
  22.         ServerSocket serverSocket = null;
  23.  
  24.         try {
  25.             serverSocket = new ServerSocket(4444);
  26.         } catch (IOException e) {
  27.             System.out.println("Could not listen on port: " + 4444 + ", " + e);
  28.             System.exit(1);
  29.         }
  30.  
  31.         Socket clientSocket = null;
  32.         try {
  33.             clientSocket = serverSocket.accept();
  34.         } catch (IOException e) {
  35.             System.out.println("Accept failed: " + 4444 + ", " + e);
  36.             System.exit(1);
  37.         }
  38.  
  39.         try {
  40.             DataInputStream is = new DataInputStream(
  41.                                  new BufferedInputStream(clientSocket.getInputStream()));
  42.             PrintStream os = new PrintStream(
  43.                              new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
  44.             KKState kks = new KKState();
  45.             String inputLine, outputLine;
  46.  
  47.             outputLine = kks.processInput(null);
  48.             os.println(outputLine);
  49.             os.flush();
  50.  
  51.             while ((inputLine = is.readLine()) != null) {
  52.                  outputLine = kks.processInput(inputLine);
  53.                  os.println(outputLine);
  54.                  os.flush();
  55.                  if (outputLine.equals("Bye."))
  56.                     break;
  57.             }
  58.             os.close();
  59.             is.close();
  60.             clientSocket.close();
  61.             serverSocket.close();
  62.  
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         }
  66.     }
  67. }
  68.