1 /* 2 * Copyright (c) 2002, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 4650679 26 * @summary Unit test for socket channels 27 * @library .. 28 */ 29 30 import java.nio.*; 31 import java.nio.channels.*; 32 import java.net.*; 33 import java.util.*; 34 35 public class Connect { 36 37 private static final long INCREMENTAL_DELAY = 30L * 1000L; 38 39 public static void main(String args[]) throws Exception { 40 test1(TestUtil.HOST); 41 try { 42 test1(TestUtil.REFUSING_HOST); 43 throw new Exception("Refused connection throws no exception"); 44 } catch (ConnectException ce) { 45 // Correct result 46 } 47 } 48 49 static void test1(String hostname) throws Exception { 50 Selector selector; 51 SocketChannel sc; 52 SelectionKey sk; 53 InetSocketAddress isa = new InetSocketAddress( 54 InetAddress.getByName (hostname), 80); 55 sc = SocketChannel.open(); 56 sc.configureBlocking(false); 57 58 selector = Selector.open(); 59 sk = sc.register(selector, SelectionKey.OP_CONNECT); 60 if (sc.connect(isa)) { 61 System.err.println("Connected immediately!"); 62 sc.close(); 63 selector.close(); 64 return; 65 } else { 66 ByteBuffer buf = ByteBuffer.allocateDirect(100); 67 buf.asCharBuffer().put(new String( 68 "The quick brown fox jumped over the lazy dog." 69 ).toCharArray()); 70 buf.flip(); 71 long startTime = System.currentTimeMillis(); 72 while(true) { 73 selector.select(INCREMENTAL_DELAY); 74 Set selectedKeys = selector.selectedKeys(); 75 76 if(selectedKeys.isEmpty()) { 77 System.err.println("Elapsed time without response: " + 78 (System.currentTimeMillis() - 79 startTime) / 1000L + " seconds."); 80 } 81 else if(!selectedKeys.contains(sk)) 82 { 83 System.err.println("Got wrong event about selection key."); 84 } else { 85 System.err.println("Got event for our selection key."); 86 if(sk.isConnectable()) { 87 if(sc.finishConnect()) { 88 if(sc.isConnected()) { 89 System.err.println("Successful connect."); 90 sk.interestOps(SelectionKey.OP_WRITE); 91 sc.write(buf); 92 } else { 93 System.err.println( 94 "Finish connect completed incorrectly."); 95 } 96 } else { 97 System.err.println( 98 "key incorrectly indicated socket channel connectable."); 99 } 100 } 101 if(sk.isWritable() && (buf.remaining() > 0)) { 102 sc.write(buf); 103 } 104 if(buf.remaining() == 0) { 105 System.err.println( 106 "SUCCESS! buffer contents were sent."); 107 sc.close(); 108 selector.close(); 109 return; 110 } 111 } 112 selectedKeys.clear(); 113 } 114 } 115 } 116 }