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

  1. /*
  2.  * @(#)Client.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.util.*;
  9. import  java.net.*;
  10. import  java.io.*;
  11.  
  12. public class Client
  13. {
  14.   private final static int BYTESPEROP= PollingServer.BYTESPEROP;
  15.   private final static int PORTNUM   = PollingServer.PORTNUM;
  16.   private final static int MAXCONN   = PollingServer.MAXCONN;
  17.  
  18.   private static Socket[] sockArr = new Socket[MAXCONN];
  19.   private static int totalConn =10;
  20.   private static int bytesToSend =1024000;
  21.   private static int connections = 0;
  22.   private static int sends = 0;
  23.  
  24.   public static void main (String args[]) {
  25.  
  26.     String host = "localhost";
  27.  
  28.     if (args.length < 1 || args.length > 3) {
  29.       System.out.println("Usage : java Client <num_connects>");
  30.       System.out.println("      | java Client <num_connects> <server_name>");
  31.       System.out.println("      | java Client <num_connects> <server_name>" +
  32.              " <max_Kbytes>");
  33.       System.exit(-1);
  34.     }
  35.  
  36.     if (args.length >= 1)
  37.       totalConn = java.lang.Integer.valueOf(args[0]).intValue();
  38.     if (args.length >= 2)
  39.       host = args[1];
  40.     if (args.length == 3)
  41.       bytesToSend = java.lang.Integer.valueOf(args[2]).intValue() * 1024;
  42.  
  43.  
  44.     if (totalConn <= 0 || totalConn > MAXCONN) {
  45.       System.out.println("Connections out of range.  Terminating.");
  46.       System.exit(-1);
  47.     }
  48.  
  49.     System.out.println("Using " + totalConn + " connections for sending " +
  50.                bytesToSend + " bytes to " + host);
  51.     
  52.  
  53.     try {
  54.       Socket ctrlSock = new Socket (host, PORTNUM);
  55.       PrintStream ctrlStream =
  56.     new PrintStream(ctrlSock.getOutputStream());
  57.       ctrlStream.println(bytesToSend);
  58.       ctrlStream.println(totalConn);
  59.       
  60.       while (connections < totalConn ) {
  61.     sockArr[connections] = new Socket (host, PORTNUM);
  62.     connections ++;
  63.       }
  64.       System.out.println("Connections made : " + connections);
  65.       
  66.       byte[] buff = new byte[BYTESPEROP];
  67.       for (int i = 0; i < BYTESPEROP; i++) // just put some junk in!
  68.     buff[i] = (byte) i;
  69.       
  70.       Random rand = new Random(5321L);
  71.       while (sends < bytesToSend/BYTESPEROP) {
  72.     int idx = java.lang.Math.abs(rand.nextInt()) % totalConn;
  73.     sockArr[idx].getOutputStream().write(buff,0,BYTESPEROP);
  74.     sends++;
  75.       }
  76.       // Wait for server to say done.
  77.       int bytes = ctrlSock.getInputStream().read(buff, 0, BYTESPEROP);
  78.       System.out.println (" Total connections : " + connections +
  79.               " Bytes sent : " + sends * BYTESPEROP +
  80.               "...Done!");
  81.     } catch (Exception e) { e.printStackTrace(); }
  82.   }
  83. }
  84.