diff --git a/libjava/ChangeLog b/libjava/ChangeLog index f8a678f3f61..9f521ef87b4 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +2003-11-25 Michael Koch + + * java/net/DatagramSocket.java + (DatagramSocket): Move binding code to bind(), simplify constructors. + * java/net/MulticastSocket.java + (MulticastSocket): Call parent constructor with null argument, + bind socket after setReuseAddress is called, simplify constructors. + 2003-11-24 Michael Koch * javax/swing/BoxLayout.java diff --git a/libjava/java/net/DatagramSocket.java b/libjava/java/net/DatagramSocket.java index 1d89d688401..59344455e27 100644 --- a/libjava/java/net/DatagramSocket.java +++ b/libjava/java/net/DatagramSocket.java @@ -138,60 +138,15 @@ public class DatagramSocket * the specified local port and address. * * @param port The local port number to bind to. - * @param laddr The local address to bind to. + * @param addr The local address to bind to. * * @exception SecurityException If a security manager exists and its * checkListen method doesn't allow the operation. * @exception SocketException If an error occurs. */ - public DatagramSocket(int port, InetAddress laddr) throws SocketException + public DatagramSocket(int port, InetAddress addr) throws SocketException { - if (port < 0 || port > 65535) - throw new IllegalArgumentException("Invalid port: " + port); - - SecurityManager s = System.getSecurityManager(); - if (s != null) - s.checkListen(port); - - String propVal = System.getProperty("impl.prefix"); - if (propVal == null || propVal.equals("")) - impl = new PlainDatagramSocketImpl(); - else - try - { - impl = (DatagramSocketImpl) Class.forName - ("java.net." + propVal + "DatagramSocketImpl").newInstance(); - } - catch (Exception e) - { - System.err.println("Could not instantiate class: java.net." + - propVal + "DatagramSocketImpl"); - impl = new PlainDatagramSocketImpl(); - } - impl.create(); - - if (laddr == null) - laddr = InetAddress.ANY_IF; - - try - { - impl.bind (port, laddr); - } - catch (SocketException exception) - { - impl.close (); - throw exception; - } - catch (RuntimeException exception) - { - impl.close (); - throw exception; - } - catch (Error error) - { - impl.close (); - throw error; - } + this(new InetSocketAddress(addr, port)); } /** @@ -209,8 +164,61 @@ public class DatagramSocket */ public DatagramSocket (SocketAddress address) throws SocketException { - this (((InetSocketAddress) address).getPort (), - ((InetSocketAddress) address).getAddress ()); + String propVal = System.getProperty("impl.prefix"); + if (propVal == null || propVal.equals("")) + impl = new PlainDatagramSocketImpl(); + else + try + { + impl = (DatagramSocketImpl) Class.forName + ("java.net." + propVal + "DatagramSocketImpl").newInstance(); + } + catch (Exception e) + { + System.err.println("Could not instantiate class: java.net." + + propVal + "DatagramSocketImpl"); + impl = new PlainDatagramSocketImpl(); + } + impl.create(); + + if (address == null) + return; + + if (! (address instanceof InetSocketAddress)) + throw new SocketException("unsupported address type"); + + InetAddress addr = ((InetSocketAddress) address).getAddress(); + int port = ((InetSocketAddress) address).getPort(); + + if (port < 0 || port > 65535) + throw new IllegalArgumentException("Invalid port: " + port); + + SecurityManager s = System.getSecurityManager(); + if (s != null) + s.checkListen(port); + + if (addr == null) + addr = InetAddress.ANY_IF; + + try + { + impl.bind(port, addr); + } + catch (SocketException exception) + { + impl.close(); + throw exception; + } + catch (RuntimeException exception) + { + impl.close(); + throw exception; + } + catch (Error error) + { + impl.close(); + throw error; + } } /** diff --git a/libjava/java/net/MulticastSocket.java b/libjava/java/net/MulticastSocket.java index 1b7b422e071..097d52e9a5c 100644 --- a/libjava/java/net/MulticastSocket.java +++ b/libjava/java/net/MulticastSocket.java @@ -80,8 +80,7 @@ public class MulticastSocket extends DatagramSocket */ public MulticastSocket() throws IOException { - super(0, null); - setReuseAddress (true); + this(new InetSocketAddress(0)); } /** @@ -95,8 +94,7 @@ public class MulticastSocket extends DatagramSocket */ public MulticastSocket(int port) throws IOException { - super(port, null); - setReuseAddress (true); + this(new InetSocketAddress(port)); } /** @@ -112,8 +110,10 @@ public class MulticastSocket extends DatagramSocket */ public MulticastSocket(SocketAddress address) throws IOException { - super(address); - setReuseAddress (true); + super((SocketAddress) null); + setReuseAddress(true); + if (address != null) + bind(address); } /**