author | bpb |
Mon, 30 Apr 2018 13:40:39 -0700 | |
changeset 49930 | 3aaaa5370999 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2002, 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 |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
25 |
* @bug 4512723 6621689 |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
26 |
* @summary Test that connect/send/receive with unbound DatagramChannel causes |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
27 |
* the channel's socket to be bound to a local address |
2 | 28 |
*/ |
29 |
||
30 |
import java.net.*; |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
31 |
import java.nio.ByteBuffer; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
32 |
import java.nio.channels.DatagramChannel; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
33 |
import java.io.IOException; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
34 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
35 |
public class NotBound { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
36 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
37 |
static void checkBound(DatagramChannel dc) throws IOException { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
38 |
if (dc.getLocalAddress() == null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
39 |
throw new RuntimeException("Not bound??"); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
40 |
} |
2 | 41 |
|
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
42 |
// starts a thread to send a datagram to the given channel once the channel |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
43 |
// is bound to a local address |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
44 |
static void wakeupWhenBound(final DatagramChannel dc) { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
45 |
Runnable wakeupTask = new Runnable() { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
46 |
public void run() { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
47 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
48 |
// poll for local address |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
49 |
InetSocketAddress local; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
50 |
do { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
51 |
Thread.sleep(50); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
52 |
local = (InetSocketAddress)dc.getLocalAddress(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
53 |
} while (local == null); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
54 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
55 |
// send message to channel to wakeup receiver |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
56 |
DatagramChannel sender = DatagramChannel.open(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
57 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
58 |
ByteBuffer bb = ByteBuffer.wrap("hello".getBytes()); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
59 |
InetAddress lh = InetAddress.getLocalHost(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
60 |
SocketAddress target = |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
61 |
new InetSocketAddress(lh, local.getPort()); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
62 |
sender.send(bb, target); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
63 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
64 |
sender.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
65 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
66 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
67 |
} catch (Exception x) { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
68 |
x.printStackTrace(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
69 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
70 |
}}; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
71 |
new Thread(wakeupTask).start(); |
2 | 72 |
} |
73 |
||
2427
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
74 |
public static void main(String[] args) throws IOException { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
75 |
DatagramChannel dc; |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
76 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
77 |
// connect |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
78 |
dc = DatagramChannel.open(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
79 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
80 |
DatagramChannel peer = DatagramChannel.open() |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
81 |
.bind(new InetSocketAddress(0)); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
82 |
int peerPort = ((InetSocketAddress)(peer.getLocalAddress())).getPort(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
83 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
84 |
dc.connect(new InetSocketAddress(InetAddress.getLocalHost(), peerPort)); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
85 |
checkBound(dc); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
86 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
87 |
peer.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
88 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
89 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
90 |
dc.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
91 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
92 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
93 |
// send |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
94 |
dc = DatagramChannel.open(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
95 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
96 |
ByteBuffer bb = ByteBuffer.wrap("ignore this".getBytes()); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
97 |
SocketAddress target = |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
98 |
new InetSocketAddress(InetAddress.getLocalHost(), 5000); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
99 |
dc.send(bb, target); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
100 |
checkBound(dc); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
101 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
102 |
dc.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
103 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
104 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
105 |
// receive (blocking) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
106 |
dc = DatagramChannel.open(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
107 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
108 |
ByteBuffer bb = ByteBuffer.allocateDirect(128); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
109 |
wakeupWhenBound(dc); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
110 |
SocketAddress sender = dc.receive(bb); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
111 |
if (sender == null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
112 |
throw new RuntimeException("Sender should not be null"); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
113 |
checkBound(dc); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
114 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
115 |
dc.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
116 |
} |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
117 |
|
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
118 |
// receive (non-blocking) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
119 |
dc = DatagramChannel.open(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
120 |
try { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
121 |
dc.configureBlocking(false); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
122 |
ByteBuffer bb = ByteBuffer.allocateDirect(128); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
123 |
SocketAddress sender = dc.receive(bb); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
124 |
if (sender != null) |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
125 |
throw new RuntimeException("Sender should be null"); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
126 |
checkBound(dc); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
127 |
} finally { |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
128 |
dc.close(); |
f35f516befc3
6621689: (dc spec) DatagramChannel.receive when channel is not bound is not specified
alanb
parents:
2
diff
changeset
|
129 |
} |
2 | 130 |
} |
131 |
} |