1 /* 2 * Copyright (c) 2001, 2018, 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 8195160 26 * @summary Test for nonblocking connect and finishConnect 27 * @requires (os.family == "linux") 28 * @library .. /test/lib 29 * @build jdk.test.lib.Utils TestServers 30 * @build RsocketTest 31 * @run main/othervm -Djava.net.preferIPv4Stack=true BasicConnect 32 */ 33 34 import java.net.*; 35 import java.nio.*; 36 import java.nio.channels.*; 37 import java.nio.channels.spi.SelectorProvider; 38 import java.util.*; 39 import jdk.net.Sockets; 40 41 /** 42 * Typically there would be more than one channel registered to select 43 * on, this test is just a very simple version with only one channel 44 * registered for the connectSelector. 45 */ 46 47 public class BasicConnect { 48 49 public static void main(String[] args) throws Exception { 50 if (!RsocketTest.isRsocketAvailable()) 51 return; 52 53 Selector connectSelector = 54 Sockets.openRdmaSelector(); 55 try (TestServers.EchoServer echoServer 56 = TestServers.EchoServer.startNewServer(100)) { 57 InetSocketAddress isa 58 = new InetSocketAddress(echoServer.getAddress(), 59 echoServer.getPort()); 60 SocketChannel sc = Sockets.openRdmaSocketChannel(); 61 sc.configureBlocking(false); 62 boolean result = sc.connect(isa); 63 if (result) { 64 System.out.println("Socket immediately connected on " 65 + System.getProperty("os.name") 66 + ": " + sc); 67 } 68 while (!result) { 69 SelectionKey connectKey = sc.register(connectSelector, 70 SelectionKey.OP_CONNECT); 71 int keysAdded = connectSelector.select(); 72 if (keysAdded > 0) { 73 Set readyKeys = connectSelector.selectedKeys(); 74 Iterator i = readyKeys.iterator(); 75 while (i.hasNext()) { 76 SelectionKey sk = (SelectionKey)i.next(); 77 i.remove(); 78 SocketChannel nextReady = (SocketChannel)sk.channel(); 79 result = nextReady.finishConnect(); 80 if (result) 81 sk.cancel(); 82 } 83 } 84 } 85 86 byte[] bs = new byte[] { (byte)0xca, (byte)0xfe, 87 (byte)0xba, (byte)0xbe }; 88 ByteBuffer bb = ByteBuffer.wrap(bs); 89 sc.configureBlocking(true); 90 sc.write(bb); 91 bb.rewind(); 92 93 ByteBuffer bb2 = ByteBuffer.allocateDirect(100); 94 int n = sc.read(bb2); 95 bb2.flip(); 96 97 sc.close(); 98 connectSelector.close(); 99 100 if (!bb.equals(bb2)) 101 throw new Exception("Echoed bytes incorrect: Sent " 102 + bb + ", got " + bb2); 103 } 104 } 105 }