changeset 4910:cb029d9b830a

7139770: MacOS JCK failures in DatagramSocket and MulticastSocket Reviewed-by: chegar
author michaelm
date Mon, 30 Jan 2012 16:17:53 +0000
parents ebeb9c8e0bb8
children 92da761e4442 3c97da8e33ec
files src/share/classes/java/net/DatagramSocket.java src/share/classes/java/net/MulticastSocket.java
diffstat 2 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/net/DatagramSocket.java	Mon Jan 30 13:52:32 2012 +0000
+++ b/src/share/classes/java/net/DatagramSocket.java	Mon Jan 30 16:17:53 2012 +0000
@@ -758,9 +758,19 @@
                 // via the impl failed.
                 boolean stop = false;
                 while (!stop) {
+                    InetAddress peekAddress = null;
+                    int peekPort = -1;
                     // peek at the packet to see who it is from.
-                    InetAddress peekAddress = new InetAddress();
-                    int peekPort = getImpl().peek(peekAddress);
+                    if (!oldImpl) {
+                        // We can use the new peekData() API
+                        DatagramPacket peekPacket = new DatagramPacket(new byte[1], 1);
+                        peekPort = getImpl().peekData(peekPacket);
+                        peekAddress = peekPacket.getAddress();
+                    } else {
+                        // this api only works for IPv4
+                        peekAddress = new InetAddress();
+                        peekPort = getImpl().peek(peekAddress);
+                    }
                     if ((!connectedAddress.equals(peekAddress)) ||
                         (connectedPort != peekPort)) {
                         // throw the packet away and silently continue
--- a/src/share/classes/java/net/MulticastSocket.java	Mon Jan 30 13:52:32 2012 +0000
+++ b/src/share/classes/java/net/MulticastSocket.java	Mon Jan 30 16:17:53 2012 +0000
@@ -82,6 +82,13 @@
  */
 public
 class MulticastSocket extends DatagramSocket {
+
+    /**
+     * Used on some platforms to record if an outgoing interface
+     * has been set for this socket.
+     */
+    private boolean interfaceSet;
+
     /**
      * Create a multicast socket.
      *
@@ -164,15 +171,6 @@
         if (bindaddr != null) {
             bind(bindaddr);
         }
-        /**
-         * required for some platforms where it's not possible to join
-         * a group without setting the interface first.
-         */
-        NetworkInterface defaultInterface = NetworkInterface.getDefault();
-
-        if (defaultInterface != null) {
-            setNetworkInterface(defaultInterface);
-        }
     }
 
     /**
@@ -308,6 +306,16 @@
             throw new SocketException("Not a multicast address");
         }
 
+        /**
+         * required for some platforms where it's not possible to join
+         * a group without setting the interface first.
+         */
+        NetworkInterface defaultInterface = NetworkInterface.getDefault();
+
+        if (!interfaceSet && defaultInterface != null) {
+            setNetworkInterface(defaultInterface);
+        }
+
         getImpl().join(mcastaddr);
     }
 
@@ -458,6 +466,7 @@
         synchronized (infLock) {
             getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf);
             infAddress = inf;
+            interfaceSet = true;
         }
     }
 
@@ -540,6 +549,7 @@
         synchronized (infLock) {
             getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf);
             infAddress = null;
+            interfaceSet = true;
         }
     }