Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/UrgentDataTest.java
41149 views
1
/*
2
* Copyright (c) 2001, 2019, 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
/*
25
* @test
26
* @bug 4092038
27
* @library /test/lib
28
* @summary TCP Urgent data support
29
* @run main UrgentDataTest
30
* @run main/othervm -Djava.net.preferIPv4Stack=true UrgentDataTest
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import jdk.test.lib.net.IPSupport;
36
37
public class UrgentDataTest {
38
39
Object opref;
40
boolean isClient, isServer;
41
String clHost;
42
ServerSocket listener;
43
Socket client, server;
44
int clPort;
45
InputStream clis, sis;
46
OutputStream clos, sos;
47
48
static void usage () {
49
System.out.println (" usage: java UrgentDataTest <runs client and server together>");
50
System.out.println (" usage: java UrgentDataTest -server <runs server alone>");
51
System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+
52
" specified server>");
53
}
54
55
public static void main (String args[]) {
56
IPSupport.throwSkippedExceptionIfNonOperational();
57
58
try {
59
UrgentDataTest test = new UrgentDataTest ();
60
if (args.length == 0) {
61
InetAddress loopback = InetAddress.getLoopbackAddress();
62
test.listener = new ServerSocket ();
63
test.listener.bind(new InetSocketAddress(loopback, 0));
64
test.isClient = true;
65
test.isServer = true;
66
test.clHost = loopback.getHostAddress();
67
test.clPort = test.listener.getLocalPort();
68
test.run();
69
} else if (args[0].equals ("-server")) {
70
test.listener = new ServerSocket (0);
71
System.out.println ("Server listening on port " + test.listener.getLocalPort());
72
test.isClient = false;
73
test.isServer = true;
74
test.run();
75
System.out.println ("Server: Completed OK");
76
} else if (args[0].equals ("-client")) {
77
test.isClient = true;
78
test.isServer = false;
79
test.clHost = args [1];
80
test.clPort = Integer.parseInt (args[2]);
81
test.run();
82
System.out.println ("Client: Completed OK");
83
} else {
84
usage ();
85
}
86
}
87
catch (ArrayIndexOutOfBoundsException e) {
88
usage();
89
}
90
catch (NumberFormatException e) {
91
usage();
92
}
93
catch (Exception e) {
94
e.printStackTrace ();
95
throw new RuntimeException ("Exception caught");
96
}
97
}
98
99
public void run () throws Exception {
100
try {
101
if (isClient) {
102
client = new Socket (clHost, clPort);
103
clis = client.getInputStream();
104
clos = client.getOutputStream();
105
client.setOOBInline (true);
106
if (client.getOOBInline() != true) {
107
throw new RuntimeException ("Setting OOBINLINE failed");
108
}
109
}
110
if (isServer) {
111
server = listener.accept ();
112
sis = server.getInputStream();
113
sos = server.getOutputStream();
114
}
115
if (isClient) {
116
clos.write ("Hello".getBytes ());
117
client.sendUrgentData (100);
118
clos.write ("world".getBytes ());
119
}
120
// read Hello world from server (during which oob byte must have been dropped)
121
String s = "Helloworld";
122
if (isServer) {
123
for (int y=0; y<s.length(); y++) {
124
int c = sis.read ();
125
if (c != (int)s.charAt (y)) {
126
throw new RuntimeException ("Unexpected character read");
127
}
128
}
129
// Do the same from server to client
130
sos.write ("Hello".getBytes ());
131
server.sendUrgentData (101);
132
sos.write ("World".getBytes ());
133
}
134
if (isClient) {
135
// read Hello world from client (during which oob byte must have been read)
136
s="Hello";
137
for (int y=0; y<s.length(); y++) {
138
int c = clis.read ();
139
if (c != (int)s.charAt (y)) {
140
throw new RuntimeException ("Unexpected character read");
141
}
142
}
143
if (clis.read() != 101) {
144
throw new RuntimeException ("OOB byte not received");
145
}
146
s="World";
147
for (int y=0; y<s.length(); y++) {
148
int c = clis.read ();
149
if (c != (int)s.charAt (y)) {
150
throw new RuntimeException ("Unexpected character read");
151
}
152
}
153
}
154
} finally {
155
if (listener != null) listener.close();
156
if (client != null) client.close ();
157
if (server != null) server.close ();
158
}
159
}
160
}
161
162