jdk/test/java/nio/channels/SocketChannel/Bind.java
changeset 5970 d4e98bbfb0be
parent 5506 202f599c92aa
child 7668 d4a77089c587
--- a/jdk/test/java/nio/channels/SocketChannel/Bind.java	Tue Jun 22 19:18:06 2010 -0700
+++ b/jdk/test/java/nio/channels/SocketChannel/Bind.java	Wed Jun 23 20:19:29 2010 +0100
@@ -31,13 +31,20 @@
 
 public class Bind {
     public static void main(String[] args) throws Exception {
+        SocketChannel sc1 = SocketChannel.open();
         try {
-            SocketChannel channel1 = SocketChannel.open();
-            channel1.socket().bind(new InetSocketAddress(5555));
-            SocketChannel channel2 = SocketChannel.open();
-            channel2.socket().bind(new InetSocketAddress(5555));
+            sc1.bind(new InetSocketAddress(0));
+            int port = sc1.socket().getLocalPort();
+            SocketChannel sc2 = SocketChannel.open();
+            try {
+                sc2.bind(new InetSocketAddress(port));
+            } finally {
+               sc2.close();
+            }
         } catch (BindException be) {
             // Correct result
+        } finally {
+            sc1.close();
         }
     }
 }