Path: blob/master/test/jdk/java/nio/channels/ServerSocketChannel/Basic.java
41154 views
/*1* Copyright (c) 2000, 2016, 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 4286936 814310025* @summary Unit test for server-socket channels26* @library ..27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;333435public class Basic {3637static PrintStream log = System.err;3839static class Server40extends TestThread41{42ServerSocketChannel ssc;43boolean block;4445Server(ServerSocketChannel ssc, boolean block) {46super("Server", Basic.log);47this.ssc = ssc;48this.block = block;49}5051void go() throws Exception {52log.println("Server: Listening "53+ (block ? "(blocking)" : "(non-blocking)"));54if (!block)55ssc.configureBlocking(false);56log.println(" " + ssc);57//log.println(" " + ssc.options());58SocketChannel sc = null;59for (;;) {60sc = ssc.accept();61if (sc != null) {62break;63}64log.println("Server: Sleeping...");65Thread.sleep(50);66}67log.println("Server: Accepted " + sc);68ByteBuffer bb = ByteBuffer.allocateDirect(100);69if (sc.read(bb) != 1)70throw new Exception("Read failed");71bb.flip();72byte b = bb.get();73log.println("Server: Read " + b + ", writing " + (b + 1));74bb.clear();75bb.put((byte)43);76bb.flip();77if (sc.write(bb) != 1)78throw new Exception("Write failed");79sc.close();80ssc.close();81log.println("Server: Finished");82}8384}8586static class Client87extends TestThread88{89int port;90boolean dally;9192Client(int port, boolean block) {93super("Client", Basic.log);94this.port = port;95this.dally = !block;96}9798public void go() throws Exception {99if (dally)100Thread.sleep(200);101InetSocketAddress isa102= new InetSocketAddress(InetAddress.getLocalHost(), port);103log.println("Client: Connecting to " + isa);104SocketChannel sc = SocketChannel.open();105sc.connect(isa);106log.println("Client: Connected");107ByteBuffer bb = ByteBuffer.allocateDirect(512);108bb.put((byte)42).flip();109log.println("Client: Writing " + bb.get(0));110if (sc.write(bb) != 1)111throw new Exception("Write failed");112bb.clear();113if (sc.read(bb) != 1)114throw new Exception("Read failed");115bb.flip();116if (bb.get() != 43)117throw new Exception("Read " + bb.get(bb.position() - 1));118log.println("Client: Read " + bb.get(0));119sc.close();120log.println("Client: Finished");121}122123}124125static void test(boolean block) throws Exception {126ServerSocketChannel ssc = ServerSocketChannel.open();127ssc.socket().setReuseAddress(true);128int port = TestUtil.bind(ssc);129Server server = new Server(ssc, block);130Client client = new Client(port, block);131server.start();132client.start();133if ((server.finish(0) & client.finish(0)) == 0)134throw new Exception("Failure");135log.println();136}137138public static void main(String[] args) throws Exception {139log.println();140test(true);141test(false);142}143144}145146147