Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/InheritTimeout.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 4508149
27
* @library /test/lib
28
* @summary Setting ServerSocket.setSoTimeout shouldn't cause
29
* the timeout to be inherited by accepted connections
30
* @run main InheritTimeout
31
* @run main/othervm -Djava.net.preferIPv4Stack=true InheritTimeout
32
*/
33
34
import java.net.*;
35
import java.io.InputStream;
36
import jdk.test.lib.net.IPSupport;
37
38
public class InheritTimeout {
39
40
class Reaper extends Thread {
41
Socket s;
42
int timeout;
43
44
Reaper(Socket s, int timeout) {
45
this.s = s;
46
this.timeout = timeout;
47
}
48
49
public void run() {
50
try {
51
Thread.currentThread().sleep(timeout);
52
s.close();
53
} catch (Exception e) {
54
}
55
}
56
}
57
58
InheritTimeout() throws Exception {
59
InetAddress ia = InetAddress.getLocalHost();
60
ServerSocket ss = new ServerSocket();
61
ss.bind(new InetSocketAddress(ia, 0));
62
ss.setSoTimeout(1000);
63
64
InetSocketAddress isa =
65
new InetSocketAddress(ia, ss.getLocalPort());
66
67
// client establishes the connection
68
Socket s1 = new Socket();
69
s1.connect(isa);
70
71
// receive the connection
72
Socket s2 = ss.accept();
73
74
// schedule reaper to close the socket in 5 seconds
75
Reaper r = new Reaper(s2, 5000);
76
r.start();
77
78
boolean readTimedOut = false;
79
try {
80
s2.getInputStream().read();
81
} catch (SocketTimeoutException te) {
82
readTimedOut = true;
83
} catch (SocketException e) {
84
if (!s2.isClosed()) {
85
throw e;
86
}
87
}
88
89
s1.close();
90
ss.close();
91
92
if (readTimedOut) {
93
throw new Exception("Unexpected SocketTimeoutException throw!");
94
}
95
}
96
97
public static void main(String args[]) throws Exception {
98
IPSupport.throwSkippedExceptionIfNonOperational();
99
new InheritTimeout();
100
}
101
}
102
103