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/net/SocketInputStream/SocketTimeout.java
38811 views
1
/*
2
* Copyright (c) 2000, 2010, 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 4158021
27
* @summary cannot distinguish Thread.interrupt and Socket.setSoTimeout exceptions
28
*/
29
30
import java.net.*;
31
import java.io.*;
32
33
public class SocketTimeout {
34
static final int TIMEOUT = 1000;
35
36
public static void main(String args[]) throws Exception {
37
InetAddress sin = InetAddress.getLocalHost();
38
Socket soc = null,soc1 = null;
39
InputStream is = null;
40
ServerSocket srv = null;
41
int port = 0;
42
43
srv = new ServerSocket(0);
44
port = srv.getLocalPort();
45
soc = new Socket(sin, port);
46
soc1 = srv.accept();
47
soc.setSoTimeout(TIMEOUT);
48
srv.setSoTimeout(TIMEOUT);
49
50
try {
51
is = soc.getInputStream();
52
is.read();
53
} catch(InterruptedIOException e) {
54
try {
55
if (! (e instanceof java.net.SocketTimeoutException))
56
throw new Exception ("Wrong exception class thrown");
57
} catch(NoClassDefFoundError e1) {
58
throw new Exception ("SocketTimeoutException: not found");
59
}
60
} finally {
61
soc.close();
62
soc1.close();
63
}
64
65
// now check accept
66
67
try {
68
srv.accept ();
69
} catch(InterruptedIOException e) {
70
try {
71
if (! (e instanceof java.net.SocketTimeoutException))
72
throw new Exception ("Wrong exception class thrown");
73
} catch(NoClassDefFoundError e1) {
74
throw new Exception ("SocketTimeoutException: not found");
75
}
76
} finally {
77
srv.close();
78
}
79
80
// Now check DatagramSocket.receive()
81
82
DatagramSocket dg = new DatagramSocket ();
83
dg.setSoTimeout (TIMEOUT);
84
85
try {
86
dg.receive (new DatagramPacket (new byte [64], 64));
87
} catch(InterruptedIOException e) {
88
try {
89
if (! (e instanceof java.net.SocketTimeoutException))
90
throw new Exception ("Wrong exception class thrown");
91
} catch(NoClassDefFoundError e1) {
92
throw new Exception ("SocketTimeoutException: not found");
93
}
94
} finally {
95
dg.close();
96
}
97
}
98
}
99
100