AdaptSocket.java revision 3261
0N/A/*
3261N/A * Copyright (c) 2001, 2010, 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 socket-channel adaptors
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/Aimport java.nio.charset.*;
0N/A
0N/A
0N/Apublic class AdaptSocket {
0N/A
0N/A static java.io.PrintStream out = System.out;
0N/A
0N/A static final int ECHO_PORT = 7;
0N/A static final int DAYTIME_PORT = 13;
0N/A static final String REMOTE_HOST = TestUtil.HOST;
0N/A static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
0N/A
0N/A static void test(String hn, int timeout, boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A out.println();
0N/A
0N/A InetSocketAddress isa
0N/A = new InetSocketAddress(InetAddress.getByName(hn),
0N/A DAYTIME_PORT);
0N/A SocketChannel sc = SocketChannel.open();
0N/A Socket so = sc.socket();
0N/A out.println("opened: " + so);
0N/A out.println(" " + sc);
0N/A
0N/A //out.println("opts: " + sc.options());
0N/A so.setTcpNoDelay(true);
0N/A //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
0N/A so.setKeepAlive(true);
0N/A so.setSoLinger(true, 42);
0N/A so.setOOBInline(true);
0N/A so.setReceiveBufferSize(512);
0N/A so.setSendBufferSize(512);
0N/A //out.println(" " + sc.options());
0N/A
0N/A if (timeout == 0)
0N/A so.connect(isa);
0N/A else {
0N/A try {
0N/A so.connect(isa, timeout);
0N/A } catch (SocketTimeoutException x) {
0N/A if (shouldTimeout) {
0N/A out.println("Connection timed out, as expected");
0N/A return;
0N/A } else {
0N/A throw x;
0N/A }
0N/A }
0N/A }
0N/A out.println("connected: " + so);
0N/A out.println(" " + sc);
0N/A byte[] bb = new byte[100];
0N/A int n = so.getInputStream().read(bb);
0N/A String s = new String(bb, 0, n - 2, "US-ASCII");
0N/A out.println(isa + " says: \"" + s + "\"");
0N/A so.shutdownInput();
0N/A out.println("ishut: " + sc);
0N/A so.shutdownOutput();
0N/A out.println("oshut: " + sc);
0N/A so.close();
0N/A out.println("closed: " + so);
0N/A out.println(" " + sc);
0N/A }
0N/A
0N/A static String dataString = "foo\r\n";
0N/A
0N/A static void testRead(Socket so, boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A String data = "foo\r\n";
0N/A so.getOutputStream().write(dataString.getBytes("US-ASCII"));
0N/A InputStream is = so.getInputStream();
0N/A try {
0N/A byte[] b = new byte[100];
0N/A int n = is.read(b);
0N/A if (n != 5)
0N/A throw new Exception("Incorrect number of bytes read: " + n);
0N/A if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
0N/A throw new Exception("Incorrect data read: " + n);
0N/A } catch (SocketTimeoutException x) {
0N/A if (shouldTimeout) {
0N/A out.println("Read timed out, as expected");
0N/A return;
0N/A }
0N/A throw x;
0N/A }
0N/A }
0N/A
0N/A static void testRead(String hn, int timeout, boolean shouldTimeout)
0N/A throws Exception
0N/A {
0N/A out.println();
0N/A
0N/A InetSocketAddress isa
0N/A = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
0N/A SocketChannel sc = SocketChannel.open();
0N/A sc.connect(isa);
0N/A Socket so = sc.socket();
0N/A out.println("connected: " + so);
0N/A out.println(" " + sc);
0N/A
0N/A if (timeout > 0)
0N/A so.setSoTimeout(timeout);
0N/A out.println("timeout: " + so.getSoTimeout());
0N/A
0N/A testRead(so, shouldTimeout);
0N/A if (!TestUtil.onME())
0N/A for (int i = 0; i < 4; i++)
0N/A testRead(so, shouldTimeout);
0N/A
0N/A sc.close();
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A test(REMOTE_HOST, 0, false);
0N/A test(REMOTE_HOST, 1000, false);
0N/A test(VERY_REMOTE_HOST, 10, true);
0N/A
0N/A testRead(REMOTE_HOST, 0, false);
0N/A testRead(REMOTE_HOST, 8000, false);
0N/A testRead(VERY_REMOTE_HOST, 10, true);
0N/A
0N/A }
0N/A
0N/A}