jdk/src/demo/solaris/jni/Poller/Client.java
author lana
Wed, 23 Nov 2016 16:16:33 +0000
changeset 42234 29a0c1a888f2
parent 25859 3317bb8137f4
permissions -rw-r--r--
Added tag jdk-9+146 for changeset d139f0e91e7e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import  java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import  java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import  java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class Client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
  private final static int BYTESPEROP= PollingServer.BYTESPEROP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  private final static int PORTNUM   = PollingServer.PORTNUM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
  private final static int MAXCONN   = PollingServer.MAXCONN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  private static Socket[] sockArr = new Socket[MAXCONN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  private static int totalConn =10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  private static int bytesToSend =1024000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  private static int connections = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  private static int sends = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
  public static void main (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    String host = "localhost";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    if (args.length < 1 || args.length > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      System.out.println("Usage : java Client <num_connects>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      System.out.println("      | java Client <num_connects> <server_name>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      System.out.println("      | java Client <num_connects> <server_name>" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                         " <max_Kbytes>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    if (args.length >= 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      totalConn = java.lang.Integer.valueOf(args[0]).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (args.length >= 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
      host = args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    if (args.length == 3)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
      bytesToSend = java.lang.Integer.valueOf(args[2]).intValue() * 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    if (totalConn <= 0 || totalConn > MAXCONN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
      System.out.println("Connections out of range.  Terminating.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
      System.exit(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    System.out.println("Using " + totalConn + " connections for sending " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                       bytesToSend + " bytes to " + host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      Socket ctrlSock = new Socket (host, PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      PrintStream ctrlStream =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        new PrintStream(ctrlSock.getOutputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
      ctrlStream.println(bytesToSend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      ctrlStream.println(totalConn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      while (connections < totalConn ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        sockArr[connections] = new Socket (host, PORTNUM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        connections ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      System.out.println("Connections made : " + connections);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      byte[] buff = new byte[BYTESPEROP];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      for (int i = 0; i < BYTESPEROP; i++) // just put some junk in!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        buff[i] = (byte) i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      Random rand = new Random(5321L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      while (sends < bytesToSend/BYTESPEROP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        int idx = java.lang.Math.abs(rand.nextInt()) % totalConn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        sockArr[idx].getOutputStream().write(buff,0,BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        sends++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      // Wait for server to say done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      int bytes = ctrlSock.getInputStream().read(buff, 0, BYTESPEROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      System.out.println (" Total connections : " + connections +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                          " Bytes sent : " + sends * BYTESPEROP +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                          "...Done!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    } catch (Exception e) { e.printStackTrace(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}