Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/GetLocalAddress.java
41149 views
1
/*
2
* Copyright (c) 1998, 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 4106601 8026245 8071424
27
* @library /test/lib
28
* @run main/othervm GetLocalAddress
29
* @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalAddress
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true GetLocalAddress
31
* @summary Test the java.net.socket.GetLocalAddress method
32
*
33
*/
34
35
import java.net.*;
36
import jdk.test.lib.net.IPSupport;
37
38
public class GetLocalAddress implements Runnable {
39
static ServerSocket ss;
40
static InetAddress addr;
41
static int port;
42
43
public static void main(String args[]) throws Exception {
44
IPSupport.throwSkippedExceptionIfNonOperational();
45
46
testBindNull();
47
48
boolean error = true;
49
int linger = 65546;
50
int value = 0;
51
addr = InetAddress.getLocalHost();
52
ss = new ServerSocket();
53
ss.bind(new InetSocketAddress(addr, 0));
54
port = ss.getLocalPort();
55
56
Thread t = new Thread(new GetLocalAddress());
57
t.start();
58
Socket soc = ss.accept();
59
60
if(addr.equals(soc.getLocalAddress())) {
61
error = false;
62
}
63
if (error)
64
throw new RuntimeException("Socket.GetLocalAddress failed.");
65
soc.close();
66
}
67
68
public void run() {
69
try {
70
Socket s = new Socket(addr, port);
71
} catch (Exception e) {
72
e.printStackTrace();
73
}
74
}
75
76
static void testBindNull() throws Exception {
77
try (Socket soc = new Socket()) {
78
soc.bind(null);
79
if (!soc.isBound())
80
throw new RuntimeException(
81
"should be bound after bind(null)");
82
if (soc.getLocalPort() <= 0)
83
throw new RuntimeException(
84
"bind(null) failed, local port: " + soc.getLocalPort());
85
if (soc.getLocalAddress() == null)
86
throw new RuntimeException(
87
"bind(null) failed, local address is null");
88
}
89
}
90
}
91
92