Path: blob/master/test/jdk/java/nio/channels/Selector/BasicConnect.java
41153 views
/*1* Copyright (c) 2001, 2018, 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 .. /test/lib27* @build jdk.test.lib.Utils TestServers28* @run main BasicConnect29*/3031import java.net.*;32import java.nio.*;33import java.nio.channels.*;34import java.nio.channels.spi.SelectorProvider;35import java.util.*;363738/**39* Typically there would be more than one channel registered to select40* on, this test is just a very simple version with only one channel41* registered for the connectSelector.42*/4344public class BasicConnect {4546public static void main(String[] args) throws Exception {47Selector connectSelector =48SelectorProvider.provider().openSelector();49try (TestServers.EchoServer echoServer50= TestServers.EchoServer.startNewServer(100)) {51InetSocketAddress isa52= new InetSocketAddress(echoServer.getAddress(),53echoServer.getPort());54SocketChannel sc = SocketChannel.open();55sc.configureBlocking(false);56boolean result = sc.connect(isa);57if (result) {58System.out.println("Socket immediately connected on "59+ System.getProperty("os.name")60+ ": " + sc);61}62while (!result) {63SelectionKey connectKey = sc.register(connectSelector,64SelectionKey.OP_CONNECT);65int keysAdded = connectSelector.select();66if (keysAdded > 0) {67Set readyKeys = connectSelector.selectedKeys();68Iterator i = readyKeys.iterator();69while (i.hasNext()) {70SelectionKey sk = (SelectionKey)i.next();71i.remove();72SocketChannel nextReady = (SocketChannel)sk.channel();73result = nextReady.finishConnect();74if (result)75sk.cancel();76}77}78}7980byte[] bs = new byte[] { (byte)0xca, (byte)0xfe,81(byte)0xba, (byte)0xbe };82ByteBuffer bb = ByteBuffer.wrap(bs);83sc.configureBlocking(true);84sc.write(bb);85bb.rewind();8687ByteBuffer bb2 = ByteBuffer.allocateDirect(100);88int n = sc.read(bb2);89bb2.flip();9091sc.close();92connectSelector.close();9394if (!bb.equals(bb2))95throw new Exception("Echoed bytes incorrect: Sent "96+ bb + ", got " + bb2);97}98}99}100101102