J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2006 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6411513 |
| 27 | * @summary java.net.DatagramSocket.receive: packet isn't received |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.util.*; |
| 32 | |
| 33 | public class B6411513 { |
| 34 | |
| 35 | public static void main( String[] args ) throws Exception { |
| 36 | Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); |
| 37 | while (nics.hasMoreElements()) { |
| 38 | NetworkInterface nic = nics.nextElement(); |
| 39 | if (nic.isUp() && !nic.isVirtual()) { |
| 40 | Enumeration<InetAddress> addrs = nic.getInetAddresses(); |
| 41 | while (addrs.hasMoreElements()) { |
| 42 | InetAddress addr = addrs.nextElement(); |
| 43 | |
| 44 | // Currently, seems there's a bug on Linux that one is |
| 45 | // unable to get IPv6 datagrams to be received by an |
| 46 | // IPv6 socket bound to any address except ::1. So filter |
| 47 | // out IPv6 address here. The test should be revisited |
| 48 | // later when aforementioned bug gets fixed. |
| 49 | if (addr instanceof Inet4Address) { |
| 50 | System.out.printf("%s : %s\n", nic.getName(), addr); |
| 51 | testConnectedUDP(addr); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* |
| 60 | * Connect a UDP socket, disconnect it, then send and recv on it. |
| 61 | * It will fail on Linux if we don't silently bind(2) again at the |
| 62 | * end of DatagramSocket.disconnect(). |
| 63 | */ |
| 64 | private static void testConnectedUDP(InetAddress addr) throws Exception { |
| 65 | try { |
| 66 | DatagramSocket s = new DatagramSocket(0, addr); |
| 67 | DatagramSocket ss = new DatagramSocket(0, addr); |
| 68 | System.out.print("\tconnect..."); |
| 69 | s.connect(ss.getLocalAddress(), ss.getLocalPort()); |
| 70 | System.out.print("disconnect..."); |
| 71 | s.disconnect(); |
| 72 | |
| 73 | byte[] data = { 0, 1, 2 }; |
| 74 | DatagramPacket p = new DatagramPacket(data, data.length, |
| 75 | s.getLocalAddress(), s.getLocalPort()); |
| 76 | s.setSoTimeout( 10000 ); |
| 77 | System.out.print("send..."); |
| 78 | s.send( p ); |
| 79 | System.out.print("recv..."); |
| 80 | s.receive( p ); |
| 81 | System.out.println("OK"); |
| 82 | |
| 83 | ss.close(); |
| 84 | s.close(); |
| 85 | } catch( Exception e ){ |
| 86 | e.printStackTrace(); |
| 87 | throw e; |
| 88 | } |
| 89 | } |
| 90 | } |