author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 49284 | a51ca91c2cde |
permissions | -rw-r--r-- |
2 | 1 |
/* |
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
7668
diff
changeset
|
25 |
* @bug 4313882 7183800 |
2 | 26 |
* @summary Test DatagramChannel's send and receive methods |
27 |
* @author Mike McCloskey |
|
28 |
*/ |
|
29 |
||
30 |
import java.io.*; |
|
31 |
import java.net.*; |
|
32 |
import java.nio.*; |
|
33 |
import java.nio.channels.*; |
|
34 |
import java.nio.charset.*; |
|
35 |
||
36 |
||
37 |
public class Connect { |
|
38 |
||
39 |
static PrintStream log = System.err; |
|
40 |
||
41 |
public static void main(String[] args) throws Exception { |
|
42 |
test(); |
|
43 |
} |
|
44 |
||
45 |
static void test() throws Exception { |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
46 |
Reactor r = new Reactor(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
47 |
Actor a = new Actor(r.port()); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
48 |
invoke(a, r); |
2 | 49 |
} |
50 |
||
51 |
static void invoke(Sprintable reader, Sprintable writer) throws Exception { |
|
52 |
||
53 |
Thread writerThread = new Thread(writer); |
|
54 |
writerThread.start(); |
|
55 |
||
56 |
Thread readerThread = new Thread(reader); |
|
57 |
readerThread.start(); |
|
58 |
||
59 |
writerThread.join(); |
|
60 |
readerThread.join(); |
|
61 |
||
62 |
reader.throwException(); |
|
63 |
writer.throwException(); |
|
64 |
} |
|
65 |
||
66 |
public interface Sprintable extends Runnable { |
|
67 |
public void throwException() throws Exception; |
|
68 |
} |
|
69 |
||
70 |
public static class Actor implements Sprintable { |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
71 |
final int port; |
2 | 72 |
Exception e = null; |
73 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
74 |
Actor(int port) { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
75 |
this.port = port; |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
76 |
} |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
77 |
|
2 | 78 |
public void throwException() throws Exception { |
79 |
if (e != null) |
|
80 |
throw e; |
|
81 |
} |
|
82 |
||
83 |
public void run() { |
|
84 |
try { |
|
85 |
DatagramChannel dc = DatagramChannel.open(); |
|
86 |
||
87 |
// Send a message |
|
88 |
ByteBuffer bb = ByteBuffer.allocateDirect(256); |
|
89 |
bb.put("hello".getBytes()); |
|
90 |
bb.flip(); |
|
91 |
InetAddress address = InetAddress.getLocalHost(); |
|
16480
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
7668
diff
changeset
|
92 |
if (address.isLoopbackAddress()) { |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
7668
diff
changeset
|
93 |
address = InetAddress.getLoopbackAddress(); |
fcad3b6ec812
7183800: TEST_BUG: Update tests to run on Ubuntu 12.04 (localhost is 127.0.1.1)
alanb
parents:
7668
diff
changeset
|
94 |
} |
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
95 |
InetSocketAddress isa = new InetSocketAddress(address, port); |
2 | 96 |
dc.connect(isa); |
97 |
dc.write(bb); |
|
98 |
||
99 |
// Try to send to some other address |
|
100 |
address = InetAddress.getLocalHost(); |
|
101 |
InetSocketAddress bogus = new InetSocketAddress(address, 3333); |
|
102 |
try { |
|
103 |
dc.send(bb, bogus); |
|
104 |
throw new RuntimeException("Allowed bogus send while connected"); |
|
49284
a51ca91c2cde
8198753: (dc) DatagramChannel throws unspecified exceptions
bpb
parents:
47216
diff
changeset
|
105 |
} catch (AlreadyConnectedException ace) { |
2 | 106 |
// Correct behavior |
107 |
} |
|
108 |
||
109 |
// Read a reply |
|
110 |
bb.flip(); |
|
111 |
dc.read(bb); |
|
112 |
bb.flip(); |
|
113 |
CharBuffer cb = Charset.forName("US-ASCII"). |
|
114 |
newDecoder().decode(bb); |
|
115 |
log.println("From Reactor: "+isa+ " said " +cb); |
|
116 |
||
117 |
// Clean up |
|
118 |
dc.disconnect(); |
|
119 |
dc.close(); |
|
120 |
} catch (Exception ex) { |
|
121 |
e = ex; |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
public static class Reactor implements Sprintable { |
|
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
127 |
final DatagramChannel dc; |
2 | 128 |
Exception e = null; |
129 |
||
5788
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
130 |
Reactor() throws IOException { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
131 |
dc = DatagramChannel.open().bind(new InetSocketAddress(0)); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
132 |
} |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
133 |
|
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
134 |
int port() { |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
135 |
return dc.socket().getLocalPort(); |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
136 |
} |
50d4ad1a780f
6961062: (dc) Several DatagramChannel tests timeout or fail with "address already in use"
alanb
parents:
5506
diff
changeset
|
137 |
|
2 | 138 |
public void throwException() throws Exception { |
139 |
if (e != null) |
|
140 |
throw e; |
|
141 |
} |
|
142 |
||
143 |
public void run() { |
|
144 |
try { |
|
145 |
// Listen for a message |
|
146 |
ByteBuffer bb = ByteBuffer.allocateDirect(100); |
|
147 |
SocketAddress sa = dc.receive(bb); |
|
148 |
bb.flip(); |
|
149 |
CharBuffer cb = Charset.forName("US-ASCII"). |
|
150 |
newDecoder().decode(bb); |
|
151 |
log.println("From Actor: "+sa+ " said " +cb); |
|
152 |
||
153 |
// Reply to sender |
|
154 |
dc.connect(sa); |
|
155 |
bb.flip(); |
|
156 |
dc.write(bb); |
|
157 |
||
158 |
// Clean up |
|
159 |
dc.disconnect(); |
|
160 |
dc.close(); |
|
161 |
} catch (Exception ex) { |
|
162 |
e = ex; |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
} |