home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / net / NetworkServer.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  4.5 KB  |  146 lines

  1. /*
  2.  * @(#)NetworkServer.java    1.9 95/12/01 James Gosling
  3.  * 
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for NON-COMMERCIAL purposes and without fee is hereby
  8.  * granted provided that this copyright notice appears in all copies. Please
  9.  * refer to the file "copyright.html" for further important copyright and
  10.  * licensing information.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  15.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  16.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  17.  * ITS DERIVATIVES.
  18.  */
  19. package sun.net;
  20.  
  21. import java.io.*;
  22. import java.net.Socket;
  23. import java.net.ServerSocket;
  24.  
  25. /**
  26.  * This is the base class for network servers.  To define a new type
  27.  * of server define a new subclass of NetworkServer with a serviceRequest
  28.  * method that services one request.  Start the server by executing:
  29.  * <pre>
  30.  *    new MyServerClass().startServer(port);
  31.  * </pre>
  32.  */
  33. public class NetworkServer implements Runnable, Cloneable {
  34.     /** Socket for communicating with client. */
  35.     public Socket clientSocket = null;
  36.     private Thread serverInstance;
  37.     private ServerSocket serverSocket;
  38.  
  39.     /** Stream for printing to the client. */
  40.     public PrintStream clientOutput;
  41.  
  42.     /** Buffered stream for reading replies from client. */
  43.     public InputStream clientInput;
  44.  
  45.     /** Close an open connection to the client. */
  46.     public void close() throws IOException {
  47.     clientSocket.close();
  48.     clientSocket = null;
  49.     clientInput = null;
  50.     clientOutput = null;
  51.     }
  52.  
  53.     /** Return client connection status */
  54.     public boolean clientIsOpen() {
  55.     return clientSocket != null;
  56.     }
  57.  
  58.     final public void run() {
  59.     if (serverSocket != null) {
  60.         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  61.         // System.out.print("Server starts " + serverSocket + "\n");
  62.         while (true) {
  63.         try {
  64.             Socket ns = serverSocket.accept();
  65. //            System.out.print("New connection " + ns + "\n");
  66.             NetworkServer n = (NetworkServer)clone();
  67.             n.serverSocket = null;
  68.             n.clientSocket = ns;
  69.             new Thread(n).start();
  70.         } catch(Exception e) {
  71.             System.out.print("Server failure\n");
  72.             e.printStackTrace();
  73.             try {
  74.             serverSocket.close();
  75.             } catch(IOException e2) {}
  76.             System.out.print("cs="+serverSocket+"\n");
  77.             break;
  78.         }
  79.         }
  80. //        close();
  81.     } else {
  82.         try {
  83.         clientOutput = new PrintStream(
  84.             new BufferedOutputStream(clientSocket.getOutputStream()),
  85.                            false);
  86.         clientInput = new BufferedInputStream(clientSocket.getInputStream());
  87.         serviceRequest();
  88.         // System.out.print("Service handler exits
  89.         // "+clientSocket+"\n");
  90.         } catch(Exception e) {
  91.         // System.out.print("Service handler failure\n");
  92.         // e.printStackTrace();
  93.         }
  94.         try {
  95.         close();
  96.         } catch(IOException e2) {}
  97.     }
  98.     }
  99.  
  100.     /** Start a server on port <i>port</i>.  It will call serviceRequest()
  101.         for each new connection. */
  102.     final public void startServer(int port) throws IOException {
  103.     serverSocket = new ServerSocket(port, 50);
  104.     serverInstance = new Thread(this);
  105.     serverInstance.start();
  106.     }
  107.  
  108.     /** Service one request.  It is invoked with the clientInput and
  109.     clientOutput streams initialized.  This method handles one client
  110.     connection. When it is done, it can simply exit. The default
  111.     server just echoes it's input. It is invoked in it's own private
  112.     thread. */
  113.     public void serviceRequest() throws IOException {
  114.     byte buf[] = new byte[300];
  115.     int n;
  116.     clientOutput.print("Echo server " + getClass().getName() + "\n");
  117.     clientOutput.flush();
  118.     while ((n = clientInput.read(buf, 0, buf.length)) >= 0) {
  119.         clientOutput.write(buf, 0, n);
  120.     }
  121.     }
  122.  
  123.     public static void main(String argv[]) {
  124.     try {
  125.         new NetworkServer ().startServer(8888);
  126.     } catch (IOException e) {
  127.         System.out.print("Server failed: "+e+"\n");
  128.     }
  129.     }
  130.  
  131.     /**
  132.      * Clone this object;
  133.      */
  134.     public Object clone() {
  135.     try { 
  136.         return super.clone();
  137.     } catch (CloneNotSupportedException e) {
  138.         // this shouldn't happen, since we are Cloneable
  139.         throw new InternalError();
  140.     }
  141.     }
  142.  
  143.     public NetworkServer () {
  144.     }
  145. }
  146.