Path: blob/master/test/jdk/java/nio/channels/SocketChannel/CloseRegisteredChannel.java
51568 views
/*1* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 4960962 621505025@summary Test if the registered SocketChannel can be closed immediately26@run main/timeout=10 CloseRegisteredChannel27*/2829import java.io.*;30import java.nio.*;31import java.nio.channels.*;32import java.net.*;3334public class CloseRegisteredChannel {35public static void main(String[] args) throws Exception {36ServerSocketChannel server = ServerSocketChannel.open();37ServerSocket s = server.socket ();38s.bind (new InetSocketAddress (0));39int port = s.getLocalPort ();40//System.out.println ("listening on port " + port);4142SocketChannel client = SocketChannel.open ();43client.connect (new InetSocketAddress (InetAddress.getLoopbackAddress(), port));44SocketChannel peer = server.accept();45peer.configureBlocking(true);4647Selector selector = Selector.open ();48client.configureBlocking (false);49SelectionKey key = client.register (50selector, SelectionKey.OP_READ, null51);52client.close();53//System.out.println ("client.isOpen = " + client.isOpen());54System.out.println ("Will hang here...");55int nb = peer.read(ByteBuffer.allocate (1024));56//System.out.println("read nb=" + nb);5758selector.close();59server.close();60}61}626364