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