Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/setReuseAddress/Restart.java
41153 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 4476378
27
* @library /test/lib
28
* @summary Check that SO_REUSEADDR allows a server to restart
29
* after a crash.
30
* @run main Restart
31
* @run main/othervm -Dsun.net.useExclusiveBind Restart
32
* @run main/othervm -Dsun.net.useExclusiveBind=true Restart
33
* @run main/othervm -Djava.net.preferIPv4Stack=true Restart
34
* @run main/othervm -Dsun.net.useExclusiveBind
35
* -Djava.net.preferIPv4Stack=true Restart
36
* @run main/othervm -Dsun.net.useExclusiveBind=true
37
* -Djava.net.preferIPv4Stack=true Restart
38
*/
39
import java.net.*;
40
import jdk.test.lib.net.IPSupport;
41
42
public class Restart {
43
44
/*
45
* Test that a server can bind to the same port after
46
* a crash -- ie: while previous connection still in
47
* TIME_WAIT state we should be able to re-bind if
48
* SO_REUSEADDR is enabled.
49
*/
50
51
public static void main(String args[]) throws Exception {
52
IPSupport.throwSkippedExceptionIfNonOperational();
53
54
InetAddress localHost = InetAddress.getLocalHost();
55
ServerSocket ss = new ServerSocket(0, 0, localHost);
56
Socket s1 = null, s2 = null;
57
try {
58
int port = ss.getLocalPort();
59
60
s1 = new Socket(localHost, port);
61
s2 = ss.accept();
62
63
// close server socket and the accepted connection
64
ss.close();
65
s2.close();
66
67
ss = new ServerSocket();
68
ss.bind( new InetSocketAddress(localHost, port) );
69
ss.close();
70
71
// close the client socket
72
s1.close();
73
} catch (BindException be) {
74
if (System.getProperty("sun.net.useExclusiveBind") != null) {
75
// exclusive bind, expected exception
76
} else {
77
throw be;
78
}
79
} finally {
80
if (ss != null) ss.close();
81
if (s1 != null) s1.close();
82
if (s2 != null) s2.close();
83
}
84
}
85
}
86
87