home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 April / DPPCPRO0499.ISO / April / Notes / 50b2wic.exe / DATA1.CAB / NotesProgramFilesJavaSupport / rt.jar / sun / net / NetworkServer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-04-23  |  2.9 KB  |  102 lines

  1. package sun.net;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.PrintStream;
  8. import java.net.ServerSocket;
  9. import java.net.Socket;
  10.  
  11. public class NetworkServer implements Runnable, Cloneable {
  12.    public Socket clientSocket;
  13.    private Thread serverInstance;
  14.    private ServerSocket serverSocket;
  15.    public PrintStream clientOutput;
  16.    public InputStream clientInput;
  17.  
  18.    public void close() throws IOException {
  19.       this.clientSocket.close();
  20.       this.clientSocket = null;
  21.       this.clientInput = null;
  22.       this.clientOutput = null;
  23.    }
  24.  
  25.    public boolean clientIsOpen() {
  26.       return this.clientSocket != null;
  27.    }
  28.  
  29.    public final void run() {
  30.       if (this.serverSocket != null) {
  31.          Thread.currentThread().setPriority(10);
  32.  
  33.          while(true) {
  34.             try {
  35.                Socket var1 = this.serverSocket.accept();
  36.                NetworkServer var2 = (NetworkServer)this.clone();
  37.                var2.serverSocket = null;
  38.                var2.clientSocket = var1;
  39.                (new Thread(var2)).start();
  40.             } catch (Exception var4) {
  41.                System.out.print("Server failure\n");
  42.                ((Throwable)var4).printStackTrace();
  43.  
  44.                try {
  45.                   this.serverSocket.close();
  46.                } catch (IOException var3) {
  47.                }
  48.  
  49.                System.out.print("cs=" + this.serverSocket + "\n");
  50.                return;
  51.             }
  52.          }
  53.       } else {
  54.          try {
  55.             this.clientOutput = new PrintStream(new BufferedOutputStream(this.clientSocket.getOutputStream()), false);
  56.             this.clientInput = new BufferedInputStream(this.clientSocket.getInputStream());
  57.             this.serviceRequest();
  58.          } catch (Exception var6) {
  59.          }
  60.  
  61.          try {
  62.             this.close();
  63.          } catch (IOException var5) {
  64.          }
  65.       }
  66.    }
  67.  
  68.    public final void startServer(int var1) throws IOException {
  69.       this.serverSocket = new ServerSocket(var1, 50);
  70.       this.serverInstance = new Thread(this);
  71.       this.serverInstance.start();
  72.    }
  73.  
  74.    public void serviceRequest() throws IOException {
  75.       byte[] var1 = new byte[300];
  76.       this.clientOutput.print("Echo server " + this.getClass().getName() + "\n");
  77.       this.clientOutput.flush();
  78.  
  79.       int var2;
  80.       while((var2 = this.clientInput.read(var1, 0, var1.length)) >= 0) {
  81.          this.clientOutput.write(var1, 0, var2);
  82.       }
  83.  
  84.    }
  85.  
  86.    public static void main(String[] var0) {
  87.       try {
  88.          (new NetworkServer()).startServer(8888);
  89.       } catch (IOException var2) {
  90.          System.out.print("Server failed: " + var2 + "\n");
  91.       }
  92.    }
  93.  
  94.    public Object clone() {
  95.       try {
  96.          return super.clone();
  97.       } catch (CloneNotSupportedException var1) {
  98.          throw new InternalError();
  99.       }
  100.    }
  101. }
  102.