jdk/test/java/nio/channels/Selector/ReadAfterConnect.java
changeset 22043 e77b5937792c
parent 7668 d4a77089c587
--- a/jdk/test/java/nio/channels/Selector/ReadAfterConnect.java	Fri Dec 13 16:15:58 2013 -0800
+++ b/jdk/test/java/nio/channels/Selector/ReadAfterConnect.java	Sat Dec 14 09:27:12 2013 +0000
@@ -27,27 +27,25 @@
  * @author kladko
  */
 
-import java.net.*;
-import java.nio.*;
-import java.nio.channels.*;
+import java.nio.channels.Selector;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.SocketChannel;
 
 public class ReadAfterConnect {
+    public static void main(String[] argv) throws Exception {
+        try (ByteServer server = new ByteServer();
+             SocketChannel sc = SocketChannel.open(server.address())) {
 
-    public static void main(String[] argv) throws Exception {
-        ByteServer server = new ByteServer(0); // server: accept connection and do nothing
-        server.start();
-        InetSocketAddress isa = new InetSocketAddress(
-                InetAddress.getByName(ByteServer.LOCALHOST), server.port());
-        Selector sel = Selector.open();
-        SocketChannel sc = SocketChannel.open();
-        sc.connect(isa);
-        sc.configureBlocking(false);
-        sc.register(sel, SelectionKey.OP_READ);
-        // Previously channel would get selected here, although there is nothing to read
-        if (sel.selectNow() != 0)
-            throw new Exception("Select returned nonzero value");
-        sc.close();
-        server.exit();
+            server.acceptConnection();
+
+            try (Selector sel = Selector.open()) {
+                sc.configureBlocking(false);
+                sc.register(sel, SelectionKey.OP_READ);
+                // Previously channel would get selected here, although there is nothing to read
+                if (sel.selectNow() != 0)
+                    throw new Exception("Select returned nonzero value");
+            }
+        }
     }
 
 }