jdk/test/java/nio/channels/Selector/ReadAfterConnect.java
changeset 22043 e77b5937792c
parent 7668 d4a77089c587
equal deleted inserted replaced
22042:98541fec9c62 22043:e77b5937792c
    25  * @bug 4629307
    25  * @bug 4629307
    26  * @summary Socket with OP_READ would get selected on connect
    26  * @summary Socket with OP_READ would get selected on connect
    27  * @author kladko
    27  * @author kladko
    28  */
    28  */
    29 
    29 
    30 import java.net.*;
    30 import java.nio.channels.Selector;
    31 import java.nio.*;
    31 import java.nio.channels.SelectionKey;
    32 import java.nio.channels.*;
    32 import java.nio.channels.SocketChannel;
    33 
    33 
    34 public class ReadAfterConnect {
    34 public class ReadAfterConnect {
       
    35     public static void main(String[] argv) throws Exception {
       
    36         try (ByteServer server = new ByteServer();
       
    37              SocketChannel sc = SocketChannel.open(server.address())) {
    35 
    38 
    36     public static void main(String[] argv) throws Exception {
    39             server.acceptConnection();
    37         ByteServer server = new ByteServer(0); // server: accept connection and do nothing
    40 
    38         server.start();
    41             try (Selector sel = Selector.open()) {
    39         InetSocketAddress isa = new InetSocketAddress(
    42                 sc.configureBlocking(false);
    40                 InetAddress.getByName(ByteServer.LOCALHOST), server.port());
    43                 sc.register(sel, SelectionKey.OP_READ);
    41         Selector sel = Selector.open();
    44                 // Previously channel would get selected here, although there is nothing to read
    42         SocketChannel sc = SocketChannel.open();
    45                 if (sel.selectNow() != 0)
    43         sc.connect(isa);
    46                     throw new Exception("Select returned nonzero value");
    44         sc.configureBlocking(false);
    47             }
    45         sc.register(sel, SelectionKey.OP_READ);
    48         }
    46         // Previously channel would get selected here, although there is nothing to read
       
    47         if (sel.selectNow() != 0)
       
    48             throw new Exception("Select returned nonzero value");
       
    49         sc.close();
       
    50         server.exit();
       
    51     }
    49     }
    52 
    50 
    53 }
    51 }