This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

FYI: Patch: java.net: DatagramSocket and MulticastSocket


Hi list,



I commited the attached patch to fix bind() issues in DatagramSocket and to 
use setReuseAddress in MulticastSocket before the socket gets bound.


Michael
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2354
diff -u -b -B -r1.2354 ChangeLog
--- ChangeLog	24 Nov 2003 16:55:40 -0000	1.2354
+++ ChangeLog	24 Nov 2003 22:59:02 -0000
@@ -1,3 +1,11 @@
+2003-11-25  Michael Koch  <konqueror@gmx.de>
+
+	* 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  <konqueror@gmx.de>
 
 	* javax/swing/BoxLayout.java
Index: java/net/DatagramSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/DatagramSocket.java,v
retrieving revision 1.29
diff -u -b -B -r1.29 DatagramSocket.java
--- java/net/DatagramSocket.java	19 Sep 2003 07:24:59 -0000	1.29
+++ java/net/DatagramSocket.java	24 Nov 2003 22:59:02 -0000
@@ -138,21 +138,32 @@
    * 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);
+    this(new InetSocketAddress(addr, port));
+  }
 
+  /**
+   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
+   * the specified local port and address.
+   *
+   * @param port The local port number to bind to.
+   * @param laddr The local address to bind to.
+   *
+   * @exception SecurityException If a security manager exists and its
+   * <code>checkListen</code> method doesn't allow the operation.
+   * @exception SocketException If an error occurs.
+   *
+   * @since 1.4
+   */
+  public DatagramSocket (SocketAddress address) throws SocketException
+  {
     String propVal = System.getProperty("impl.prefix");
     if (propVal == null || propVal.equals(""))
       impl = new PlainDatagramSocketImpl();
@@ -170,47 +181,44 @@
 	}
     impl.create();
 
-    if (laddr == null)
-      laddr = InetAddress.ANY_IF;
+    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, laddr);
+        impl.bind(port, addr);
       }
     catch (SocketException exception)
       {
-        impl.close ();
+        impl.close();
         throw exception;
       }
     catch (RuntimeException exception)
       {
-        impl.close ();
+        impl.close();
         throw exception;
       }
     catch (Error error)
       {
-        impl.close ();
+        impl.close();
         throw error;
       }
-  }
-
-  /**
-   * Initializes a new instance of <code>DatagramSocket</code> that binds to 
-   * the specified local port and address.
-   *
-   * @param port The local port number to bind to.
-   * @param laddr The local address to bind to.
-   *
-   * @exception SecurityException If a security manager exists and its
-   * <code>checkListen</code> method doesn't allow the operation.
-   * @exception SocketException If an error occurs.
-   *
-   * @since 1.4
-   */
-  public DatagramSocket (SocketAddress address) throws SocketException
-  {
-    this (((InetSocketAddress) address).getPort (),
-          ((InetSocketAddress) address).getAddress ());
   }
   
   /**
Index: java/net/MulticastSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/MulticastSocket.java,v
retrieving revision 1.23
diff -u -b -B -r1.23 MulticastSocket.java
--- java/net/MulticastSocket.java	19 Sep 2003 07:24:59 -0000	1.23
+++ java/net/MulticastSocket.java	24 Nov 2003 22:59:02 -0000
@@ -80,8 +80,7 @@
    */
   public MulticastSocket() throws IOException
   {
-    super(0, null);
-    setReuseAddress (true);
+    this(new InetSocketAddress(0));
   }
 
   /**
@@ -95,8 +94,7 @@
    */
   public MulticastSocket(int port) throws IOException
   {
-    super(port, null);
-    setReuseAddress (true);
+    this(new InetSocketAddress(port));
   }
 
   /**
@@ -112,8 +110,10 @@
    */
   public MulticastSocket(SocketAddress address) throws IOException
   {
-    super(address);
-    setReuseAddress (true);
+    super((SocketAddress) null);
+    setReuseAddress(true);
+    if (address != null)
+      bind(address);
   }
   
   /**

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]