jdk/test/java/nio/channels/SocketChannel/Bind.java
changeset 5970 d4e98bbfb0be
parent 5506 202f599c92aa
child 7668 d4a77089c587
equal deleted inserted replaced
5968:4e9dd25279c7 5970:d4e98bbfb0be
    29 import java.net.*;
    29 import java.net.*;
    30 import java.nio.channels.*;
    30 import java.nio.channels.*;
    31 
    31 
    32 public class Bind {
    32 public class Bind {
    33     public static void main(String[] args) throws Exception {
    33     public static void main(String[] args) throws Exception {
       
    34         SocketChannel sc1 = SocketChannel.open();
    34         try {
    35         try {
    35             SocketChannel channel1 = SocketChannel.open();
    36             sc1.bind(new InetSocketAddress(0));
    36             channel1.socket().bind(new InetSocketAddress(5555));
    37             int port = sc1.socket().getLocalPort();
    37             SocketChannel channel2 = SocketChannel.open();
    38             SocketChannel sc2 = SocketChannel.open();
    38             channel2.socket().bind(new InetSocketAddress(5555));
    39             try {
       
    40                 sc2.bind(new InetSocketAddress(port));
       
    41             } finally {
       
    42                sc2.close();
       
    43             }
    39         } catch (BindException be) {
    44         } catch (BindException be) {
    40             // Correct result
    45             // Correct result
       
    46         } finally {
       
    47             sc1.close();
    41         }
    48         }
    42     }
    49     }
    43 }
    50 }