Path: blob/master/test/jdk/java/nio/channels/Selector/BasicAccept.java
41153 views
/*1* Copyright (c) 2000, 2010, 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 Selector with ServerSocketChannels25* @library ..26*/2728import java.io.*;29import java.net.*;30import java.nio.*;31import java.nio.channels.*;32import java.nio.channels.spi.SelectorProvider;33import java.util.*;343536public class BasicAccept {3738static void server(ServerSocketChannel ssc) throws Exception {39Selector acceptSelector = Selector.open();40try {41ssc.configureBlocking(false);42SelectionKey acceptKey43= ssc.register(acceptSelector, SelectionKey.OP_ACCEPT);44for (;;) {45int n = acceptSelector.select();46if (Thread.interrupted())47break;48if (n == 0)49continue;50Set<SelectionKey> readyKeys = acceptSelector.selectedKeys();51Iterator<SelectionKey> i = readyKeys.iterator();52while (i.hasNext()) {53SelectionKey sk = i.next();54i.remove();55ServerSocketChannel nextReady56= (ServerSocketChannel)sk.channel();57SocketChannel sc = nextReady.accept();58ByteBuffer bb = ByteBuffer.wrap(new byte[] { 42 });59sc.write(bb);60sc.close();61}62}63} finally {64acceptSelector.close();65}66}6768private static class Server extends TestThread {69final ServerSocketChannel ssc;70Server() throws IOException {71super("Server", System.err);72this.ssc = ServerSocketChannel.open()73.bind(new InetSocketAddress(0));74}75int port() {76return ssc.socket().getLocalPort();77}78void go() throws Exception {79try {80server(ssc);81} finally {82ssc.close();83}84}85}8687static void client(int port) throws Exception {88// Get a connection from the server89InetAddress lh = InetAddress.getLocalHost();90InetSocketAddress isa91= new InetSocketAddress(lh, port);92int connectFailures = 0;93boolean result = false;94SocketChannel sc = SocketChannel.open();95for (;;) {96try {97result = sc.connect(isa);98break;99} catch (java.net.ConnectException e) {100connectFailures++;101if (connectFailures > 30)102throw new RuntimeException("Cannot connect");103Thread.currentThread().sleep(100);104sc = SocketChannel.open();105}106}107if (result) {108System.err.println("Connected");109} else {110// Only happens when server and client are on separate machines111System.err.println("Connection pending...");112connectFailures = 0;113while (!result) {114try {115result = sc.finishConnect();116if (!result)117System.err.println("Not finished");118Thread.sleep(50);119} catch (java.net.ConnectException e) {120Thread.sleep(100);121connectFailures++;122if (connectFailures > 30)123throw new RuntimeException("Cannot finish connecting");124}125}126System.err.println("Finished connecting");127}128129ByteBuffer bb = ByteBuffer.allocateDirect(1024);130if (sc.read(bb) < 0)131throw new RuntimeException("Failed to read from server");132if (bb.get(0) != 42)133throw new RuntimeException("Read wrong byte from server");134System.err.println("Read from server");135sc.close();136}137138public static void main(String[] args) throws Exception {139Server server = new Server();140server.start();141try {142client(server.port());143} finally {144server.interrupt();145server.finish(2000);146}147}148149}150151152