Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/Selector/BasicConnect.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @summary Test nonblocking connect and finishConnect25* @bug 445777626* @library ..27*/2829import java.net.*;30import java.nio.*;31import java.nio.channels.*;32import java.nio.channels.spi.SelectorProvider;33import java.util.*;343536/**37* Typically there would be more than one channel registered to select38* on, this test is just a very simple version with only one channel39* registered for the connectSelector.40*/4142public class BasicConnect {4344public static void main(String[] args) throws Exception {45Selector connectSelector =46SelectorProvider.provider().openSelector();47try (TestServers.EchoServer echoServer48= TestServers.EchoServer.startNewServer(100)) {49InetSocketAddress isa50= new InetSocketAddress(echoServer.getAddress(),51echoServer.getPort());52SocketChannel sc = SocketChannel.open();53sc.configureBlocking(false);54boolean result = sc.connect(isa);55if (result) {56System.out.println("Socket immediately connected on "57+ System.getProperty("os.name")58+ ": " + sc);59}60while (!result) {61SelectionKey connectKey = sc.register(connectSelector,62SelectionKey.OP_CONNECT);63int keysAdded = connectSelector.select();64if (keysAdded > 0) {65Set readyKeys = connectSelector.selectedKeys();66Iterator i = readyKeys.iterator();67while (i.hasNext()) {68SelectionKey sk = (SelectionKey)i.next();69i.remove();70SocketChannel nextReady = (SocketChannel)sk.channel();71result = nextReady.finishConnect();72if (result)73sk.cancel();74}75}76}7778byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,79(byte)0xba, (byte)0xbe };80ByteBuffer bb = ByteBuffer.wrap(bs);81sc.configureBlocking(true);82sc.write(bb);83bb.rewind();8485ByteBuffer bb2 = ByteBuffer.allocateDirect(100);86int n = sc.read(bb2);87bb2.flip();8889sc.close();90connectSelector.close();9192if (!bb.equals(bb2))93throw new Exception("Echoed bytes incorrect: Sent "94+ bb + ", got " + bb2);95}96}97}9899100