2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2006, 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 |
/*
|
|
25 |
* @test
|
|
26 |
* @bug 6411513
|
|
27 |
* @summary java.net.DatagramSocket.receive: packet isn't received
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.net.*;
|
58680
|
31 |
import java.nio.ByteBuffer;
|
|
32 |
import java.nio.channels.DatagramChannel;
|
2
|
33 |
import java.util.*;
|
58680
|
34 |
import static java.lang.System.out;
|
2
|
35 |
|
|
36 |
public class B6411513 {
|
|
37 |
|
|
38 |
public static void main( String[] args ) throws Exception {
|
|
39 |
Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
|
|
40 |
while (nics.hasMoreElements()) {
|
|
41 |
NetworkInterface nic = nics.nextElement();
|
|
42 |
if (nic.isUp() && !nic.isVirtual()) {
|
|
43 |
Enumeration<InetAddress> addrs = nic.getInetAddresses();
|
|
44 |
while (addrs.hasMoreElements()) {
|
|
45 |
InetAddress addr = addrs.nextElement();
|
|
46 |
|
|
47 |
// Currently, seems there's a bug on Linux that one is
|
|
48 |
// unable to get IPv6 datagrams to be received by an
|
|
49 |
// IPv6 socket bound to any address except ::1. So filter
|
|
50 |
// out IPv6 address here. The test should be revisited
|
|
51 |
// later when aforementioned bug gets fixed.
|
|
52 |
if (addr instanceof Inet4Address) {
|
58680
|
53 |
out.printf("%s : %s\n", nic.getName(), addr);
|
2
|
54 |
testConnectedUDP(addr);
|
|
55 |
}
|
|
56 |
}
|
|
57 |
}
|
|
58 |
}
|
|
59 |
}
|
|
60 |
|
|
61 |
|
|
62 |
/*
|
|
63 |
* Connect a UDP socket, disconnect it, then send and recv on it.
|
|
64 |
* It will fail on Linux if we don't silently bind(2) again at the
|
|
65 |
* end of DatagramSocket.disconnect().
|
|
66 |
*/
|
|
67 |
private static void testConnectedUDP(InetAddress addr) throws Exception {
|
|
68 |
try {
|
|
69 |
DatagramSocket s = new DatagramSocket(0, addr);
|
|
70 |
DatagramSocket ss = new DatagramSocket(0, addr);
|
58680
|
71 |
out.println("localaddress: " + s.getLocalSocketAddress());
|
|
72 |
out.println("\tconnect...");
|
2
|
73 |
s.connect(ss.getLocalAddress(), ss.getLocalPort());
|
58680
|
74 |
out.println("localaddress: " + s.getLocalSocketAddress());
|
|
75 |
out.println("disconnect...");
|
2
|
76 |
s.disconnect();
|
58680
|
77 |
out.println("localaddress: " + s.getLocalSocketAddress());
|
2
|
78 |
|
|
79 |
byte[] data = { 0, 1, 2 };
|
|
80 |
DatagramPacket p = new DatagramPacket(data, data.length,
|
|
81 |
s.getLocalAddress(), s.getLocalPort());
|
|
82 |
s.setSoTimeout( 10000 );
|
58680
|
83 |
out.print("send...");
|
2
|
84 |
s.send( p );
|
58680
|
85 |
out.print("recv...");
|
2
|
86 |
s.receive( p );
|
58680
|
87 |
out.println("OK");
|
2
|
88 |
|
|
89 |
ss.close();
|
|
90 |
s.close();
|
|
91 |
} catch( Exception e ){
|
|
92 |
e.printStackTrace();
|
|
93 |
throw e;
|
|
94 |
}
|
|
95 |
}
|
58680
|
96 |
|
|
97 |
// Tests with DatagramChannel
|
|
98 |
// private static void testConnectedUDPNIO(InetAddress addr) throws Exception {
|
|
99 |
// DatagramChannel s = DatagramChannel.open();
|
|
100 |
// s.bind(new InetSocketAddress(addr, 0));
|
|
101 |
// DatagramChannel ss = DatagramChannel.open();
|
|
102 |
// ss.bind(new InetSocketAddress(addr, 0));
|
|
103 |
// out.println("localaddress: " + s.getLocalAddress());
|
|
104 |
// out.println("\tconnect...");
|
|
105 |
// s.connect(ss.getLocalAddress());
|
|
106 |
// out.println("localaddress: " + s.getLocalAddress());
|
|
107 |
// out.println("disconnect...");
|
|
108 |
// s.disconnect();
|
|
109 |
// out.println("localaddress: " + s.getLocalAddress());
|
|
110 |
//
|
|
111 |
// byte[] data = {0, 1, 2};
|
|
112 |
// out.print("send...");
|
|
113 |
// s.send(ByteBuffer.wrap(data), s.getLocalAddress());
|
|
114 |
// out.print("recv...");
|
|
115 |
// s.receive(ByteBuffer.allocate(100));
|
|
116 |
// out.println("OK");
|
|
117 |
//
|
|
118 |
// ss.close();
|
|
119 |
// s.close();
|
|
120 |
// }
|
2
|
121 |
}
|