author | prr |
Thu, 18 Dec 2014 10:45:45 -0800 | |
changeset 29908 | 83e2c403fefd |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 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 | 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 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 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 |
||
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 | 41 |
import java.io.*; |
42 |
import java.net.*; |
|
43 |
import java.lang.Byte; |
|
44 |
||
45 |
/** |
|
46 |
* Simple Java "server" using a single thread to handle each connection. |
|
47 |
*/ |
|
48 |
||
49 |
public class SimpleServer |
|
50 |
{ |
|
51 |
private final static int BYTESPEROP= PollingServer.BYTESPEROP; |
|
52 |
private final static int PORTNUM = PollingServer.PORTNUM; |
|
53 |
private final static int MAXCONN = PollingServer.MAXCONN; |
|
54 |
||
55 |
/* |
|
56 |
* This synchronization object protects access to certain |
|
57 |
* data (bytesRead,eventsToProcess) by concurrent Consumer threads. |
|
58 |
*/ |
|
59 |
private final static Object eventSync = new Object(); |
|
60 |
||
61 |
private static InputStream[] instr = new InputStream[MAXCONN]; |
|
62 |
private static int bytesRead; |
|
63 |
private static int bytesToRead; |
|
64 |
||
65 |
public SimpleServer() { |
|
66 |
Socket[] sockArr = new Socket[MAXCONN]; |
|
67 |
long timestart, timestop; |
|
68 |
int bytes; |
|
69 |
int totalConn=0; |
|
70 |
||
71 |
||
72 |
System.out.println ("Serv: Initializing port " + PORTNUM); |
|
73 |
try { |
|
74 |
||
75 |
ServerSocket skMain = new ServerSocket (PORTNUM); |
|
76 |
||
77 |
bytesRead = 0; |
|
78 |
Socket ctrlSock = skMain.accept(); |
|
79 |
||
80 |
BufferedReader ctrlReader = |
|
81 |
new BufferedReader(new InputStreamReader(ctrlSock.getInputStream())); |
|
82 |
String ctrlString = ctrlReader.readLine(); |
|
83 |
bytesToRead = Integer.valueOf(ctrlString).intValue(); |
|
84 |
ctrlString = ctrlReader.readLine(); |
|
85 |
totalConn = Integer.valueOf(ctrlString).intValue(); |
|
86 |
||
87 |
System.out.println("Receiving " + bytesToRead + " bytes from " + |
|
88 |
totalConn + " client connections"); |
|
89 |
||
90 |
timestart = System.currentTimeMillis(); |
|
91 |
||
92 |
/* |
|
93 |
* Take connections, spawn off connection handling threads |
|
94 |
*/ |
|
95 |
ConnHandler[] connHA = new ConnHandler[MAXCONN]; |
|
96 |
int conn = 0; |
|
97 |
while ( conn < totalConn ) { |
|
98 |
Socket sock = skMain.accept(); |
|
99 |
connHA[conn] = new ConnHandler(sock.getInputStream()); |
|
100 |
connHA[conn].start(); |
|
101 |
conn++; |
|
102 |
} |
|
103 |
||
104 |
while ( bytesRead < bytesToRead ) { |
|
105 |
java.lang.Thread.sleep(500); |
|
106 |
} |
|
107 |
timestop = System.currentTimeMillis(); |
|
108 |
System.out.println("Time for all reads (" + totalConn + |
|
109 |
" sockets) : " + (timestop-timestart)); |
|
110 |
// Tell the client it can now go away |
|
111 |
byte[] buff = new byte[BYTESPEROP]; |
|
112 |
ctrlSock.getOutputStream().write(buff,0,BYTESPEROP); |
|
113 |
} catch (Exception exc) { exc.printStackTrace(); } |
|
114 |
} |
|
115 |
||
116 |
/* |
|
117 |
* main ... just create invoke the SimpleServer constructor. |
|
118 |
*/ |
|
119 |
public static void main (String args[]) |
|
120 |
{ |
|
121 |
SimpleServer server = new SimpleServer(); |
|
122 |
} |
|
123 |
||
124 |
/* |
|
125 |
* Connection Handler inner class...one of these per client connection. |
|
126 |
*/ |
|
127 |
class ConnHandler extends Thread { |
|
128 |
private InputStream instr; |
|
129 |
public ConnHandler(InputStream inputStr) { instr = inputStr; } |
|
130 |
||
131 |
public void run() { |
|
132 |
try { |
|
133 |
int bytes; |
|
134 |
byte[] buff = new byte[BYTESPEROP]; |
|
135 |
||
136 |
while ( bytesRead < bytesToRead ) { |
|
137 |
bytes = instr.read (buff, 0, BYTESPEROP); |
|
138 |
if (bytes > 0 ) { |
|
139 |
synchronized(eventSync) { |
|
140 |
bytesRead += bytes; |
|
141 |
} |
|
142 |
/* |
|
143 |
* Any real server would do some synchronized and some |
|
144 |
* unsynchronized work on behalf of the client, and |
|
145 |
* most likely send some data back...but this is a |
|
146 |
* gross oversimplification. |
|
147 |
*/ |
|
148 |
} |
|
149 |
else { |
|
150 |
if (bytesRead < bytesToRead) |
|
151 |
System.out.println("instr.read returned : " + bytes); |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
catch (Exception e) {e.printStackTrace();} |
|
156 |
} |
|
157 |
} |
|
158 |
} |