Path: blob/master/test/jdk/java/nio/channels/SocketChannel/CloseTimeoutChannel.java
41154 views
/*1* Copyright (c) 2005, 2019, 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 4726957 4724030 623295425@summary Test if the SocketChannel with timeout set can be closed immediately26@run main/timeout=20 CloseTimeoutChannel27*/2829import java.io.*;30import java.nio.*;31import java.nio.channels.*;32import java.net.*;3334public class CloseTimeoutChannel {35public static void main(String args[]) throws Exception {36int port = -1;37try {38ServerSocketChannel listener=ServerSocketChannel.open();39listener.socket().bind(new InetSocketAddress(0));40port = listener.socket().getLocalPort();41AcceptorThread thread=new AcceptorThread(listener);42thread.start();43} catch (IOException e) {44System.out.println("Mysterious IO problem");45e.printStackTrace();46System.exit(1);47}4849//Establish connection. Bug only happens if we open with channel.50try {51System.out.println("Establishing connection");52Socket socket=SocketChannel.open(53new InetSocketAddress(InetAddress.getLoopbackAddress(), port)).socket();54OutputStream out=socket.getOutputStream();55InputStream in=socket.getInputStream();5657System.out.println("1. Writing byte 1");58out.write((byte)1);5960int n=read(socket, in);61System.out.println("Read byte "+n+"\n");6263System.out.println("3. Writing byte 3");64out.write((byte)3);6566System.out.println("Closing");67socket.close();68} catch (IOException e) {69System.out.println("Mysterious IO problem");70e.printStackTrace();71System.exit(1);72}73}7475/** Reads one byte from in, which must be s.getInputStream. */76private static int read(Socket s, InputStream in) throws IOException {77try {78s.setSoTimeout(8000); //causes a bug!79return in.read();80} finally {81s.setSoTimeout(0);82}8384}8586/** Server thread */87static class AcceptorThread extends Thread {88final String INDENT="\t\t\t\t";89ServerSocketChannel _listener;90/** @param listener MUST be bound to a port */91AcceptorThread(ServerSocketChannel listener) {92_listener=listener;93}9495public void run() {96try {97try {98Thread.sleep(100);99} catch (InterruptedException e) { }100101System.out.println(INDENT+"Listening on port "+102_listener.socket().getLocalPort());103ByteBuffer buf=ByteBuffer.allocate(5);104Socket client=_listener.accept().socket();;105System.out.println(INDENT+"Accepted client");106107OutputStream out=client.getOutputStream();108InputStream in=client.getInputStream();109110int n=in.read();111System.out.println(INDENT+"Read byte "+n+"\n");112113System.out.println(INDENT+"2. Writing byte 2");114out.write((byte)2);115116n=in.read();117System.out.println(INDENT+"Read byte "+n+"\n");118119n=in.read();120System.out.println(INDENT+"Read byte "121+(n<0 ? "EOF" : Integer.toString(n)));122123System.out.println(INDENT+"Closing");124client.close();125} catch (IOException e) {126System.out.println(INDENT+"Error accepting!");127} finally {128try { _listener.close(); } catch (IOException ignore) { }129}130}131}132}133134135