home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jni / Poller / SimpleServer.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  3.4 KB  |  127 lines

  1. /*
  2.  * @(#)SimpleServer.java    1.5 01/12/03
  3.  *
  4.  * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5.  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.lang.Byte;
  11.  
  12. /**
  13.  * Simple Java "server" using a single thread to handle each connection.
  14.  */
  15.  
  16. public class SimpleServer
  17. {
  18.   private final static int BYTESPEROP= PollingServer.BYTESPEROP;
  19.   private final static int PORTNUM   = PollingServer.PORTNUM;
  20.   private final static int MAXCONN   = PollingServer.MAXCONN;
  21.  
  22.   /*
  23.    * This synchronization object protects access to certain
  24.    * data (bytesRead,eventsToProcess) by concurrent Consumer threads.
  25.    */
  26.   private final static Object eventSync = new Object();
  27.  
  28.   private static InputStream[] instr = new InputStream[MAXCONN];
  29.   private static int bytesRead;
  30.   private static int bytesToRead;
  31.  
  32.   public SimpleServer() {
  33.     Socket[] sockArr = new Socket[MAXCONN];
  34.     long timestart, timestop;
  35.     int bytes;
  36.     int totalConn=0;
  37.  
  38.  
  39.     System.out.println ("Serv: Initializing port " + PORTNUM);
  40.     try {
  41.  
  42.       ServerSocket skMain = new ServerSocket (PORTNUM);
  43.  
  44.       bytesRead = 0;
  45.       Socket ctrlSock = skMain.accept();
  46.  
  47.       BufferedReader ctrlReader =
  48.     new BufferedReader(new InputStreamReader(ctrlSock.getInputStream()));
  49.       String ctrlString = ctrlReader.readLine();
  50.       bytesToRead = Integer.valueOf(ctrlString).intValue();
  51.       ctrlString = ctrlReader.readLine();
  52.       totalConn = Integer.valueOf(ctrlString).intValue();
  53.  
  54.       System.out.println("Receiving " + bytesToRead + " bytes from " +
  55.              totalConn + " client connections");
  56.       
  57.       timestart = System.currentTimeMillis();
  58.  
  59.       /*
  60.        * Take connections, spawn off connection handling threads
  61.        */
  62.       ConnHandler[] connHA = new ConnHandler[MAXCONN];
  63.       int conn = 0;
  64.       while ( conn < totalConn ) {
  65.       Socket sock = skMain.accept();
  66.       connHA[conn] = new ConnHandler(sock.getInputStream());
  67.       connHA[conn].start();
  68.       conn++;
  69.       }
  70.  
  71.       while ( bytesRead < bytesToRead ) {
  72.       java.lang.Thread.sleep(500);
  73.       }
  74.       timestop = System.currentTimeMillis();
  75.       System.out.println("Time for all reads (" + totalConn +
  76.              " sockets) : " + (timestop-timestart));
  77.       // Tell the client it can now go away
  78.       byte[] buff = new byte[BYTESPEROP];
  79.       ctrlSock.getOutputStream().write(buff,0,BYTESPEROP);
  80.     } catch (Exception exc) { exc.printStackTrace(); }
  81.   }
  82.  
  83.   /*
  84.    * main ... just create invoke the SimpleServer constructor.
  85.    */
  86.   public static void main (String args[])
  87.   {
  88.     SimpleServer server = new SimpleServer();
  89.   }
  90.  
  91.   /*
  92.    * Connection Handler inner class...one of these per client connection.
  93.    */
  94.   class ConnHandler extends Thread {
  95.     private InputStream instr;
  96.     public ConnHandler(InputStream inputStr) { instr = inputStr; }
  97.  
  98.     public void run() {
  99.       try {
  100.     int bytes;
  101.     byte[] buff = new byte[BYTESPEROP];
  102.     
  103.     while ( bytesRead < bytesToRead ) {
  104.       bytes = instr.read (buff, 0, BYTESPEROP);
  105.       if (bytes > 0 ) {
  106.         synchronized(eventSync) {
  107.           bytesRead += bytes; 
  108.         }
  109.         /*
  110.          * Any real server would do some synchronized and some
  111.          * unsynchronized work on behalf of the client, and
  112.          * most likely send some data back...but this is a
  113.          * gross oversimplification.
  114.          */
  115.       }
  116.       else {
  117.         if (bytesRead < bytesToRead)
  118.           System.out.println("instr.read returned : " + bytes);
  119.       }
  120.     }
  121.       }
  122.       catch (Exception e) {e.printStackTrace();}
  123.     }
  124.   }
  125. }  
  126.  
  127.