Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/Connect.java
38828 views
/*1* Copyright (c) 2002, 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* @bug 465067925* @summary Unit test for socket channels26* @library ..27*/2829import java.net.*;30import java.nio.*;31import java.nio.channels.*;32import java.util.*;3334public class Connect {3536private static final long INCREMENTAL_DELAY = 30L * 1000L;3738public static void main(String args[]) throws Exception {39try (TestServers.EchoServer echoServer40= TestServers.EchoServer.startNewServer(1000)) {41test1(echoServer);42}43try {44TestServers.RefusingServer refusingServer45= TestServers.RefusingServer.startNewServer();46test1(refusingServer);47throw new Exception("Refused connection throws no exception");48} catch (ConnectException ce) {49// Correct result50}51}5253static void test1(TestServers.AbstractServer server) throws Exception {54Selector selector;55SocketChannel sc;56SelectionKey sk;57InetSocketAddress isa = new InetSocketAddress(58server.getAddress(), server.getPort());59sc = SocketChannel.open();60sc.configureBlocking(false);6162selector = Selector.open();63sk = sc.register(selector, SelectionKey.OP_CONNECT);64if (sc.connect(isa)) {65System.err.println("Connected immediately!");66sc.close();67selector.close();68return;69} else {70ByteBuffer buf = ByteBuffer.allocateDirect(100);71buf.asCharBuffer().put(new String(72"The quick brown fox jumped over the lazy dog."73).toCharArray());74buf.flip();75long startTime = System.currentTimeMillis();76while(true) {77selector.select(INCREMENTAL_DELAY);78Set selectedKeys = selector.selectedKeys();7980if(selectedKeys.isEmpty()) {81System.err.println("Elapsed time without response: " +82(System.currentTimeMillis() -83startTime) / 1000L + " seconds.");84}85else if(!selectedKeys.contains(sk))86{87System.err.println("Got wrong event about selection key.");88} else {89System.err.println("Got event for our selection key.");90if(sk.isConnectable()) {91if(sc.finishConnect()) {92if(sc.isConnected()) {93System.err.println("Successful connect.");94sk.interestOps(SelectionKey.OP_WRITE);95sc.write(buf);96} else {97System.err.println(98"Finish connect completed incorrectly.");99}100} else {101System.err.println(102"key incorrectly indicated socket channel connectable.");103}104}105if(sk.isWritable() && (buf.remaining() > 0)) {106sc.write(buf);107}108if(buf.remaining() == 0) {109System.err.println(110"SUCCESS! buffer contents were sent.");111sc.close();112selector.close();113return;114}115}116selectedKeys.clear();117}118}119}120}121122123