summaryrefslogtreecommitdiff
path: root/java/net/MulticastSocket.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/net/MulticastSocket.java')
-rw-r--r--java/net/MulticastSocket.java46
1 files changed, 39 insertions, 7 deletions
diff --git a/java/net/MulticastSocket.java b/java/net/MulticastSocket.java
index 03bdf1e77..2841192db 100644
--- a/java/net/MulticastSocket.java
+++ b/java/net/MulticastSocket.java
@@ -202,13 +202,41 @@ public class MulticastSocket extends DatagramSocket
{
if (isClosed())
throw new SocketException("socket is closed");
-
- Enumeration e = netIf.getInetAddresses();
-
- if (! e.hasMoreElements())
- throw new SocketException("no network devices found");
-
- InetAddress address = (InetAddress) e.nextElement();
+
+ InetAddress address;
+ if (netIf != null)
+ out:
+ {
+ Enumeration e = netIf.getInetAddresses();
+ if (getLocalAddress() instanceof Inet4Address)
+ {
+ // Search for a IPv4 address.
+ while (e.hasMoreElements())
+ {
+ address = (InetAddress) e.nextElement();
+ if (address instanceof Inet4Address)
+ break out;
+ }
+ throw new SocketException("interface " + netIf.getName() + " has no IPv6 address");
+ }
+ else if (getLocalAddress() instanceof Inet6Address)
+ {
+ // Search for a IPv6 address.
+ while (e.hasMoreElements())
+ {
+ address = (InetAddress) e.nextElement();
+ if (address instanceof Inet6Address)
+ break out;
+ }
+ throw new SocketException("interface " + netIf.getName() + " has no IPv6 address");
+ }
+ else
+ throw new SocketException("interface " + netIf.getName() + " has no suitable IP address");
+ }
+ else
+ address = InetAddress.ANY_IF;
+
+
getImpl().setOption(SocketOptions.IP_MULTICAST_IF, address);
}
@@ -230,6 +258,10 @@ public class MulticastSocket extends DatagramSocket
InetAddress address =
(InetAddress) getImpl().getOption(SocketOptions.IP_MULTICAST_IF);
+
+ if (address.isAnyLocalAddress())
+ return NetworkInterface.createAnyInterface();
+
NetworkInterface netIf = NetworkInterface.getByInetAddress(address);
return netIf;