From 97b8365cafc3a344a22d3980b8ed885f5c6d8357 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 9 Jan 2007 19:58:05 +0000 Subject: Merged gcj-eclipse branch to trunk. From-SVN: r120621 --- libjava/classpath/java/net/MulticastSocket.java | 46 +++++++++++++++++++++---- 1 file changed, 39 insertions(+), 7 deletions(-) (limited to 'libjava/classpath/java/net/MulticastSocket.java') diff --git a/libjava/classpath/java/net/MulticastSocket.java b/libjava/classpath/java/net/MulticastSocket.java index 03bdf1e..2841192 100644 --- a/libjava/classpath/java/net/MulticastSocket.java +++ b/libjava/classpath/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; -- cgit v1.1