1 /* 2 * Copyright (c) 2001, 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 * @summary Test SocketChannel.finishConnect 26 * @library .. 27 */ 28 29 import java.net.*; 30 import java.nio.*; 31 import java.nio.channels.*; 32 import java.nio.channels.spi.SelectorProvider; 33 import java.nio.charset.*; 34 import java.util.*; 35 36 37 public class FinishConnect { 38 39 static final int DAYTIME_PORT = 13; 40 static final String DAYTIME_HOST = TestUtil.HOST; 41 42 public static void main(String[] args) throws Exception { 43 test1(true, true); 44 test1(true, false); 45 test1(false, true); 46 test1(false, false); 47 test2(); 48 } 49 50 static void test1(boolean select, boolean setBlocking) throws Exception { 51 InetSocketAddress isa 52 = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), 53 DAYTIME_PORT); 54 SocketChannel sc = SocketChannel.open(); 55 sc.configureBlocking(false); 56 boolean connected = sc.connect(isa); 57 int attempts = 0; 58 59 try { 60 sc.connect(isa); 61 throw new RuntimeException("Allowed another connect call"); 62 } catch (IllegalStateException ise) { 63 // Correct behavior 64 } 65 66 if (setBlocking) 67 sc.configureBlocking(true); 68 69 if (!connected && select && !setBlocking) { 70 Selector selector = SelectorProvider.provider().openSelector(); 71 sc.register(selector, SelectionKey.OP_CONNECT); 72 while (!connected) { 73 int keysAdded = selector.select(100); 92 connected = sc.finishConnect(); 93 } 94 95 ByteBuffer bb = ByteBuffer.allocateDirect(100); 96 int bytesRead = 0; 97 int totalRead = 0; 98 while (totalRead < 20) { 99 bytesRead = sc.read(bb); 100 if (bytesRead > 0) 101 totalRead += bytesRead; 102 if (bytesRead < 0) 103 throw new RuntimeException("Message shorter than expected"); 104 } 105 bb.position(bb.position() - 2); // Drop CRLF 106 bb.flip(); 107 CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); 108 System.err.println(isa + " says: \"" + cb + "\""); 109 sc.close(); 110 } 111 112 static void test2() throws Exception { 113 InetSocketAddress isa 114 = new InetSocketAddress(InetAddress.getByName(DAYTIME_HOST), 115 DAYTIME_PORT); 116 boolean done = false; 117 int globalAttempts = 0; 118 while (!done) { 119 if (globalAttempts++ > 50) 120 throw new RuntimeException("Failed to connect"); 121 SocketChannel sc = SocketChannel.open(); 122 sc.configureBlocking(false); 123 boolean connected = sc.connect(isa); 124 int localAttempts = 0; 125 while (!connected) { 126 if (localAttempts++ > 500) 127 throw new RuntimeException("Failed to connect"); 128 connected = sc.finishConnect(); 129 if (connected) { 130 done = true; 131 break; 132 } 133 Thread.sleep(10); 134 } 135 sc.close(); 136 } 137 } 138 139 } | 1 /* 2 * Copyright (c) 2001, 2012, 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 * @summary Test SocketChannel.finishConnect 26 * @library .. 27 */ 28 29 import java.net.*; 30 import java.nio.*; 31 import java.nio.channels.*; 32 import java.nio.channels.spi.SelectorProvider; 33 import java.nio.charset.*; 34 import java.util.*; 35 36 37 public class FinishConnect { 38 39 public static void main(String[] args) throws Exception { 40 try (TestUtil.DayTimeServer dayTimeServer = 41 TestUtil.DayTimeServer.startNewServer(100)) { 42 test1(dayTimeServer, true, true); 43 test1(dayTimeServer, true, false); 44 test1(dayTimeServer, false, true); 45 test1(dayTimeServer, false, false); 46 test2(dayTimeServer); 47 } 48 } 49 50 static void test1(TestUtil.DayTimeServer daytimeServer, 51 boolean select, boolean setBlocking) throws Exception { 52 InetSocketAddress isa = new InetSocketAddress(daytimeServer.getAddress(), 53 daytimeServer.getPort()); 54 SocketChannel sc = SocketChannel.open(); 55 sc.configureBlocking(false); 56 boolean connected = sc.connect(isa); 57 int attempts = 0; 58 59 try { 60 sc.connect(isa); 61 throw new RuntimeException("Allowed another connect call"); 62 } catch (IllegalStateException ise) { 63 // Correct behavior 64 } 65 66 if (setBlocking) 67 sc.configureBlocking(true); 68 69 if (!connected && select && !setBlocking) { 70 Selector selector = SelectorProvider.provider().openSelector(); 71 sc.register(selector, SelectionKey.OP_CONNECT); 72 while (!connected) { 73 int keysAdded = selector.select(100); 92 connected = sc.finishConnect(); 93 } 94 95 ByteBuffer bb = ByteBuffer.allocateDirect(100); 96 int bytesRead = 0; 97 int totalRead = 0; 98 while (totalRead < 20) { 99 bytesRead = sc.read(bb); 100 if (bytesRead > 0) 101 totalRead += bytesRead; 102 if (bytesRead < 0) 103 throw new RuntimeException("Message shorter than expected"); 104 } 105 bb.position(bb.position() - 2); // Drop CRLF 106 bb.flip(); 107 CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb); 108 System.err.println(isa + " says: \"" + cb + "\""); 109 sc.close(); 110 } 111 112 static void test2(TestUtil.DayTimeServer daytimeServer) throws Exception { 113 InetSocketAddress isa = new InetSocketAddress(daytimeServer.getAddress(), 114 daytimeServer.getPort()); 115 boolean done = false; 116 int globalAttempts = 0; 117 int connectSuccess = 0; 118 while (!done) { 119 // When using a local daytime server it is not always possible 120 // to get a pending connection, as sc.connect(isa) may always 121 // return true. 122 // So we're going to throw the exception only if there was 123 // at least 1 case where we did not manage to connect. 124 if (globalAttempts++ > 50) { 125 if (globalAttempts == connectSuccess + 1) { 126 System.err.println("Can't fully test on " 127 + System.getProperty("os.name")); 128 break; 129 } 130 throw new RuntimeException("Failed to connect"); 131 } 132 SocketChannel sc = SocketChannel.open(); 133 sc.configureBlocking(false); 134 boolean connected = sc.connect(isa); 135 int localAttempts = 0; 136 while (!connected) { 137 if (localAttempts++ > 500) 138 throw new RuntimeException("Failed to connect"); 139 connected = sc.finishConnect(); 140 if (connected) { 141 done = true; 142 break; 143 } 144 Thread.sleep(10); 145 } 146 if (connected) { 147 connectSuccess++; 148 } 149 sc.close(); 150 } 151 } 152 153 } |