Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/ServerSocketChannel/Basic.java
38828 views
/*1* Copyright (c) 2000, 2001, 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 Unit test for server-socket channels25* @library ..26*/2728import java.io.*;29import java.net.*;30import java.nio.*;31import java.nio.channels.*;323334public class Basic {3536static PrintStream log = System.err;3738static class Server39extends TestThread40{41ServerSocketChannel ssc;42boolean block;4344Server(ServerSocketChannel ssc, boolean block) {45super("Server", Basic.log);46this.ssc = ssc;47this.block = block;48}4950void go() throws Exception {51log.println("Server: Listening "52+ (block ? "(blocking)" : "(non-blocking)"));53if (!block)54ssc.configureBlocking(false);55log.println(" " + ssc);56//log.println(" " + ssc.options());57SocketChannel sc = null;58for (;;) {59sc = ssc.accept();60if (sc != null) {61break;62}63log.println("Server: Sleeping...");64Thread.sleep(50);65}66log.println("Server: Accepted " + sc);67ByteBuffer bb = ByteBuffer.allocateDirect(100);68if (sc.read(bb) != 1)69throw new Exception("Read failed");70bb.flip();71byte b = bb.get();72log.println("Server: Read " + b + ", writing " + (b + 1));73bb.clear();74bb.put((byte)43);75bb.flip();76if (sc.write(bb) != 1)77throw new Exception("Write failed");78sc.close();79ssc.close();80log.println("Server: Finished");81}8283}8485static class Client86extends TestThread87{88int port;89boolean dally;9091Client(int port, boolean block) {92super("Client", Basic.log);93this.port = port;94this.dally = !block;95}9697public void go() throws Exception {98if (dally)99Thread.sleep(200);100InetSocketAddress isa101= new InetSocketAddress(InetAddress.getLocalHost(), port);102log.println("Client: Connecting to " + isa);103SocketChannel sc = SocketChannel.open();104sc.connect(isa);105log.println("Client: Connected");106ByteBuffer bb = ByteBuffer.allocateDirect(512);107bb.put((byte)42).flip();108log.println("Client: Writing " + bb.get(0));109if (sc.write(bb) != 1)110throw new Exception("Write failed");111bb.clear();112if (sc.read(bb) != 1)113throw new Exception("Read failed");114bb.flip();115if (bb.get() != 43)116throw new Exception("Read " + bb.get(bb.position() - 1));117log.println("Client: Read " + bb.get(0));118sc.close();119log.println("Client: Finished");120}121122}123124static void test(boolean block) throws Exception {125ServerSocketChannel ssc = ServerSocketChannel.open();126ssc.socket().setReuseAddress(true);127int port = TestUtil.bind(ssc);128Server server = new Server(ssc, block);129Client client = new Client(port, block);130server.start();131client.start();132if ((server.finish(2000) & client.finish(100)) == 0)133throw new Exception("Failure");134log.println();135}136137public static void main(String[] args) throws Exception {138log.println();139test(true);140test(false);141}142143}144145146