Path: blob/master/test/jdk/java/nio/channels/Selector/Connect.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* @bug 451162425* @summary Test Making lots of Selectors26* @library .. /test/lib27* @build jdk.test.lib.Utils TestServers28* @run main Connect29*/3031import java.net.*;32import java.nio.*;33import java.nio.channels.*;34import java.nio.channels.spi.SelectorProvider;35import java.util.*;3637public class Connect {3839static int success = 0;40static int LIMIT = 100;4142public static void main(String[] args) throws Exception {43try (TestServers.DayTimeServer daytimeServer44= TestServers.DayTimeServer.startNewServer(50)) {45scaleTest(daytimeServer);46}47}4849static void scaleTest(TestServers.DayTimeServer daytimeServer)50throws Exception51{52InetAddress myAddress = daytimeServer.getAddress();53InetSocketAddress isa54= new InetSocketAddress(myAddress, daytimeServer.getPort());5556for (int j=0; j<LIMIT; j++) {57SocketChannel sc = SocketChannel.open();58sc.configureBlocking(false);59boolean connected = sc.connect(isa);60if (!connected) {61Selector RSelector = SelectorProvider.provider().openSelector();62SelectionKey RKey = sc.register (RSelector, SelectionKey.OP_CONNECT);63while (!connected) {64int keysAdded = RSelector.select(100);65if (keysAdded > 0) {66Set<SelectionKey> readyKeys = RSelector.selectedKeys();67Iterator<SelectionKey> i = readyKeys.iterator();68while (i.hasNext()) {69SelectionKey sk = i.next();70SocketChannel nextReady = (SocketChannel)sk.channel();71connected = nextReady.finishConnect();72}73readyKeys.clear();74}75}76RSelector.close();77}78readAndClose(sc);79}80}8182static void readAndClose(SocketChannel sc) throws Exception {83ByteBuffer bb = ByteBuffer.allocateDirect(100);84int n = 0;85while (n == 0) // Note this is not a rigorous check for done reading86n = sc.read(bb);87//bb.position(bb.position() - 2);88//bb.flip();89//CharBuffer cb = Charset.forName("US-ASCII").newDecoder().decode(bb);90//System.out.println("Received: \"" + cb + "\"");91sc.close();92success++;93}94}959697