0N/A/*
2362N/A * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @summary Unit test for server-socket channels
0N/A * @library ..
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/Apublic class Basic {
0N/A
0N/A static PrintStream log = System.err;
0N/A
0N/A static class Server
0N/A extends TestThread
0N/A {
0N/A ServerSocketChannel ssc;
0N/A boolean block;
0N/A
0N/A Server(ServerSocketChannel ssc, boolean block) {
0N/A super("Server", Basic.log);
0N/A this.ssc = ssc;
0N/A this.block = block;
0N/A }
0N/A
0N/A void go() throws Exception {
0N/A log.println("Server: Listening "
0N/A + (block ? "(blocking)" : "(non-blocking)"));
0N/A if (!block)
0N/A ssc.configureBlocking(false);
0N/A log.println(" " + ssc);
0N/A //log.println(" " + ssc.options());
0N/A SocketChannel sc = null;
0N/A for (;;) {
0N/A sc = ssc.accept();
0N/A if (sc != null) {
0N/A break;
0N/A }
0N/A log.println("Server: Sleeping...");
0N/A Thread.sleep(50);
0N/A }
0N/A log.println("Server: Accepted " + sc);
0N/A ByteBuffer bb = ByteBuffer.allocateDirect(100);
0N/A if (sc.read(bb) != 1)
0N/A throw new Exception("Read failed");
0N/A bb.flip();
0N/A byte b = bb.get();
0N/A log.println("Server: Read " + b + ", writing " + (b + 1));
0N/A bb.clear();
0N/A bb.put((byte)43);
0N/A bb.flip();
0N/A if (sc.write(bb) != 1)
0N/A throw new Exception("Write failed");
0N/A sc.close();
0N/A ssc.close();
0N/A log.println("Server: Finished");
0N/A }
0N/A
0N/A }
0N/A
0N/A static class Client
0N/A extends TestThread
0N/A {
0N/A int port;
0N/A boolean dally;
0N/A
0N/A Client(int port, boolean block) {
0N/A super("Client", Basic.log);
0N/A this.port = port;
0N/A this.dally = !block;
0N/A }
0N/A
0N/A public void go() throws Exception {
0N/A if (dally)
0N/A Thread.sleep(200);
0N/A InetSocketAddress isa
0N/A = new InetSocketAddress(InetAddress.getLocalHost(), port);
0N/A log.println("Client: Connecting to " + isa);
0N/A SocketChannel sc = SocketChannel.open();
0N/A sc.connect(isa);
0N/A log.println("Client: Connected");
0N/A ByteBuffer bb = ByteBuffer.allocateDirect(512);
0N/A bb.put((byte)42).flip();
0N/A log.println("Client: Writing " + bb.get(0));
0N/A if (sc.write(bb) != 1)
0N/A throw new Exception("Write failed");
0N/A bb.clear();
0N/A if (sc.read(bb) != 1)
0N/A throw new Exception("Read failed");
0N/A bb.flip();
0N/A if (bb.get() != 43)
0N/A throw new Exception("Read " + bb.get(bb.position() - 1));
0N/A log.println("Client: Read " + bb.get(0));
0N/A sc.close();
0N/A log.println("Client: Finished");
0N/A }
0N/A
0N/A }
0N/A
0N/A static void test(boolean block) throws Exception {
0N/A ServerSocketChannel ssc = ServerSocketChannel.open();
0N/A ssc.socket().setReuseAddress(true);
0N/A int port = TestUtil.bind(ssc);
0N/A Server server = new Server(ssc, block);
0N/A Client client = new Client(port, block);
0N/A server.start();
0N/A client.start();
0N/A if ((server.finish(2000) & client.finish(100)) == 0)
0N/A throw new Exception("Failure");
0N/A log.println();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A log.println();
0N/A test(true);
0N/A test(false);
0N/A }
0N/A
0N/A}