You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by tr...@apache.org on 2005/10/28 09:24:05 UTC

svn commit: r329145 - /directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java

Author: trustin
Date: Fri Oct 28 00:24:00 2005
New Revision: 329145

URL: http://svn.apache.org/viewcvs?rev=329145&view=rev
Log:
* Refactored AvailablePortFinder
** All open server socket ports are set to be reusable (SO_REUSEADDR)

Modified:
    directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java

Modified: directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java?rev=329145&r1=329144&r2=329145&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/util/AvailablePortFinder.java Fri Oct 28 00:24:00 2005
@@ -90,36 +90,10 @@
 
         for (int i = fromPort; i <= MAX_PORT_NUMBER; i++)
         {
-            ServerSocket ss = null;
-            DatagramSocket ds = null;
-            try
+            if( available( i ) )
             {
-                ss = new ServerSocket(i);
-                ds = new DatagramSocket(i);
                 return i;
             }
-            catch (IOException e)
-            {
-            }
-            finally
-            {
-                if (ds != null)
-                {
-                    ds.close();
-                }
-
-                if (ss != null)
-                {
-                    try
-                    {
-                        ss.close();
-                    }
-                    catch (IOException e)
-                    {
-                        /* should not be thrown */
-                    }
-                }
-            }
         }
 
         throw new NoSuchElementException("Could not find an available port "
@@ -138,23 +112,31 @@
             throw new IllegalArgumentException( "Invalid start port: " + port );
         }
 
-        ServerSocket s = null;
+        ServerSocket ss = null;
+        DatagramSocket ds = null;
         try
         {
-            s = new ServerSocket( port );
+            ss = new ServerSocket( port );
+            ss.setReuseAddress( true );
+            ds = new DatagramSocket( port );
+            ds.setReuseAddress( true );
             return true;
         }
-        catch ( IOException e )
+        catch (IOException e)
         {
-            return false;
         }
         finally
         {
-            if ( s != null )
+            if (ds != null)
+            {
+                ds.close();
+            }
+
+            if (ss != null)
             {
                 try
                 {
-                    s.close();
+                    ss.close();
                 }
                 catch (IOException e)
                 {
@@ -162,6 +144,8 @@
                 }
             }
         }
+        
+        return false;
     }
 
     /**