J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | @bug 4726957 4724030 6232954 |
| 26 | @summary Test if the SocketChannel with timeout set can be closed immediately |
| 27 | @run main/timeout=20 CloseTimeoutChannel |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.nio.*; |
| 32 | import java.nio.channels.*; |
| 33 | import java.net.*; |
| 34 | |
| 35 | public class CloseTimeoutChannel { |
| 36 | final static int PORT=6347; |
| 37 | public static void main(String args[]) throws Exception { |
| 38 | try { |
| 39 | ServerSocketChannel listener=ServerSocketChannel.open(); |
| 40 | listener.socket().bind(new InetSocketAddress(PORT)); |
| 41 | AcceptorThread thread=new AcceptorThread(listener); |
| 42 | thread.start(); |
| 43 | } catch (IOException e) { |
| 44 | System.out.println("Mysterious IO problem"); |
| 45 | e.printStackTrace(); |
| 46 | System.exit(1); |
| 47 | } |
| 48 | |
| 49 | //Establish connection. Bug only happens if we open with channel. |
| 50 | try { |
| 51 | System.out.println("Establishing connection"); |
| 52 | Socket socket=SocketChannel.open( |
| 53 | new InetSocketAddress("127.0.0.1", PORT)).socket(); |
| 54 | OutputStream out=socket.getOutputStream(); |
| 55 | InputStream in=socket.getInputStream(); |
| 56 | |
| 57 | System.out.println("1. Writing byte 1"); |
| 58 | out.write((byte)1); |
| 59 | |
| 60 | int n=read(socket, in); |
| 61 | System.out.println("Read byte "+n+"\n"); |
| 62 | |
| 63 | System.out.println("3. Writing byte 3"); |
| 64 | out.write((byte)3); |
| 65 | |
| 66 | System.out.println("Closing"); |
| 67 | socket.close(); |
| 68 | } catch (IOException e) { |
| 69 | System.out.println("Mysterious IO problem"); |
| 70 | e.printStackTrace(); |
| 71 | System.exit(1); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** Reads one byte from in, which must be s.getInputStream. */ |
| 76 | private static int read(Socket s, InputStream in) throws IOException { |
| 77 | try { |
| 78 | s.setSoTimeout(8000); //causes a bug! |
| 79 | return in.read(); |
| 80 | } finally { |
| 81 | s.setSoTimeout(0); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /** Server thread */ |
| 87 | static class AcceptorThread extends Thread { |
| 88 | final String INDENT="\t\t\t\t"; |
| 89 | ServerSocketChannel _listener; |
| 90 | /** @param listener MUST be bound to a port */ |
| 91 | AcceptorThread(ServerSocketChannel listener) { |
| 92 | _listener=listener; |
| 93 | } |
| 94 | |
| 95 | public void run() { |
| 96 | try { |
| 97 | try { |
| 98 | Thread.sleep(100); |
| 99 | } catch (InterruptedException e) { } |
| 100 | |
| 101 | System.out.println(INDENT+"Listening on port "+ PORT); |
| 102 | ByteBuffer buf=ByteBuffer.allocate(5); |
| 103 | Socket client=_listener.accept().socket();; |
| 104 | System.out.println(INDENT+"Accepted client"); |
| 105 | |
| 106 | OutputStream out=client.getOutputStream(); |
| 107 | InputStream in=client.getInputStream(); |
| 108 | |
| 109 | int n=in.read(); |
| 110 | System.out.println(INDENT+"Read byte "+n+"\n"); |
| 111 | |
| 112 | System.out.println(INDENT+"2. Writing byte 2"); |
| 113 | out.write((byte)2); |
| 114 | |
| 115 | n=in.read(); |
| 116 | System.out.println(INDENT+"Read byte "+n+"\n"); |
| 117 | |
| 118 | n=in.read(); |
| 119 | System.out.println(INDENT+"Read byte " |
| 120 | +(n<0 ? "EOF" : Integer.toString(n))); |
| 121 | |
| 122 | System.out.println(INDENT+"Closing"); |
| 123 | client.close(); |
| 124 | } catch (IOException e) { |
| 125 | System.out.println(INDENT+"Error accepting!"); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |