Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/ServerSocketChannel/Basic.java
38828 views
1
/*
2
* Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @summary Unit test for server-socket channels
26
* @library ..
27
*/
28
29
import java.io.*;
30
import java.net.*;
31
import java.nio.*;
32
import java.nio.channels.*;
33
34
35
public class Basic {
36
37
static PrintStream log = System.err;
38
39
static class Server
40
extends TestThread
41
{
42
ServerSocketChannel ssc;
43
boolean block;
44
45
Server(ServerSocketChannel ssc, boolean block) {
46
super("Server", Basic.log);
47
this.ssc = ssc;
48
this.block = block;
49
}
50
51
void go() throws Exception {
52
log.println("Server: Listening "
53
+ (block ? "(blocking)" : "(non-blocking)"));
54
if (!block)
55
ssc.configureBlocking(false);
56
log.println(" " + ssc);
57
//log.println(" " + ssc.options());
58
SocketChannel sc = null;
59
for (;;) {
60
sc = ssc.accept();
61
if (sc != null) {
62
break;
63
}
64
log.println("Server: Sleeping...");
65
Thread.sleep(50);
66
}
67
log.println("Server: Accepted " + sc);
68
ByteBuffer bb = ByteBuffer.allocateDirect(100);
69
if (sc.read(bb) != 1)
70
throw new Exception("Read failed");
71
bb.flip();
72
byte b = bb.get();
73
log.println("Server: Read " + b + ", writing " + (b + 1));
74
bb.clear();
75
bb.put((byte)43);
76
bb.flip();
77
if (sc.write(bb) != 1)
78
throw new Exception("Write failed");
79
sc.close();
80
ssc.close();
81
log.println("Server: Finished");
82
}
83
84
}
85
86
static class Client
87
extends TestThread
88
{
89
int port;
90
boolean dally;
91
92
Client(int port, boolean block) {
93
super("Client", Basic.log);
94
this.port = port;
95
this.dally = !block;
96
}
97
98
public void go() throws Exception {
99
if (dally)
100
Thread.sleep(200);
101
InetSocketAddress isa
102
= new InetSocketAddress(InetAddress.getLocalHost(), port);
103
log.println("Client: Connecting to " + isa);
104
SocketChannel sc = SocketChannel.open();
105
sc.connect(isa);
106
log.println("Client: Connected");
107
ByteBuffer bb = ByteBuffer.allocateDirect(512);
108
bb.put((byte)42).flip();
109
log.println("Client: Writing " + bb.get(0));
110
if (sc.write(bb) != 1)
111
throw new Exception("Write failed");
112
bb.clear();
113
if (sc.read(bb) != 1)
114
throw new Exception("Read failed");
115
bb.flip();
116
if (bb.get() != 43)
117
throw new Exception("Read " + bb.get(bb.position() - 1));
118
log.println("Client: Read " + bb.get(0));
119
sc.close();
120
log.println("Client: Finished");
121
}
122
123
}
124
125
static void test(boolean block) throws Exception {
126
ServerSocketChannel ssc = ServerSocketChannel.open();
127
ssc.socket().setReuseAddress(true);
128
int port = TestUtil.bind(ssc);
129
Server server = new Server(ssc, block);
130
Client client = new Client(port, block);
131
server.start();
132
client.start();
133
if ((server.finish(2000) & client.finish(100)) == 0)
134
throw new Exception("Failure");
135
log.println();
136
}
137
138
public static void main(String[] args) throws Exception {
139
log.println();
140
test(true);
141
test(false);
142
}
143
144
}
145
146