AdaptSocket.java revision 5155
1612N/A/*
1879N/A * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
1612N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1612N/A *
1612N/A * This code is free software; you can redistribute it and/or modify it
1612N/A * under the terms of the GNU General Public License version 2 only, as
1612N/A * published by the Free Software Foundation.
1612N/A *
1612N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1612N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1612N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1612N/A * version 2 for more details (a copy is included in the LICENSE file that
1612N/A * accompanied this code).
1612N/A *
1612N/A * You should have received a copy of the GNU General Public License version
1612N/A * 2 along with this work; if not, write to the Free Software Foundation,
1612N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1612N/A *
1612N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1612N/A * or visit www.oracle.com if you need additional information or have any
1612N/A * questions.
1612N/A */
1612N/A
1612N/A/* @test
1612N/A * @summary Unit test for socket-channel adaptors
1879N/A * @library ..
1879N/A */
1879N/A
1879N/Aimport java.io.*;
1879N/Aimport java.net.*;
1879N/Aimport java.nio.channels.*;
1612N/A
1612N/A
1612N/Apublic class AdaptSocket {
1612N/A
1612N/A static java.io.PrintStream out = System.out;
1612N/A
1612N/A static final int ECHO_PORT = 7;
1612N/A static final int DAYTIME_PORT = 13;
1612N/A static final String REMOTE_HOST = TestUtil.HOST;
1612N/A static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
1612N/A
1612N/A static void test(String hn, int timeout, boolean shouldTimeout)
1612N/A throws Exception
1612N/A {
1612N/A out.println();
1612N/A
1612N/A InetSocketAddress isa
1612N/A = new InetSocketAddress(InetAddress.getByName(hn),
1612N/A DAYTIME_PORT);
1612N/A SocketChannel sc = SocketChannel.open();
1612N/A Socket so = sc.socket();
1612N/A out.println("opened: " + so);
1612N/A out.println(" " + sc);
1612N/A
1612N/A //out.println("opts: " + sc.options());
1612N/A so.setTcpNoDelay(true);
1612N/A //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
1612N/A so.setKeepAlive(true);
1612N/A so.setSoLinger(true, 42);
1612N/A so.setOOBInline(true);
1612N/A so.setReceiveBufferSize(512);
1612N/A so.setSendBufferSize(512);
1612N/A //out.println(" " + sc.options());
1612N/A
1612N/A if (timeout == 0)
1612N/A so.connect(isa);
1612N/A else {
1612N/A try {
1612N/A so.connect(isa, timeout);
1612N/A } catch (SocketTimeoutException x) {
1612N/A if (shouldTimeout) {
1612N/A out.println("Connection timed out, as expected");
1612N/A return;
1612N/A } else {
1612N/A throw x;
1612N/A }
1612N/A }
1612N/A }
1612N/A out.println("connected: " + so);
1612N/A out.println(" " + sc);
1612N/A byte[] bb = new byte[100];
1612N/A int n = so.getInputStream().read(bb);
1612N/A String s = new String(bb, 0, n - 2, "US-ASCII");
1612N/A out.println(isa + " says: \"" + s + "\"");
1612N/A so.shutdownInput();
1612N/A out.println("ishut: " + sc);
1612N/A so.shutdownOutput();
1612N/A out.println("oshut: " + sc);
1612N/A so.close();
1612N/A out.println("closed: " + so);
1612N/A out.println(" " + sc);
1612N/A }
1612N/A
1612N/A static String dataString = "foo\r\n";
1612N/A
1612N/A static void testRead(Socket so, boolean shouldTimeout)
1612N/A throws Exception
1612N/A {
1612N/A String data = "foo\r\n";
1612N/A so.getOutputStream().write(dataString.getBytes("US-ASCII"));
1612N/A InputStream is = so.getInputStream();
1612N/A try {
1612N/A byte[] b = new byte[100];
1612N/A int n = is.read(b);
1612N/A if (n != 5)
1612N/A throw new Exception("Incorrect number of bytes read: " + n);
1612N/A if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
1612N/A throw new Exception("Incorrect data read: " + n);
1612N/A } catch (SocketTimeoutException x) {
1612N/A if (shouldTimeout) {
1612N/A out.println("Read timed out, as expected");
1612N/A return;
1612N/A }
1612N/A throw x;
1612N/A }
1612N/A }
1612N/A
1612N/A static void testRead(String hn, int timeout, boolean shouldTimeout)
1612N/A throws Exception
1612N/A {
1612N/A out.println();
1612N/A
1612N/A InetSocketAddress isa
1612N/A = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
1612N/A SocketChannel sc = SocketChannel.open();
1612N/A sc.connect(isa);
1612N/A Socket so = sc.socket();
1612N/A out.println("connected: " + so);
1612N/A out.println(" " + sc);
1612N/A
1612N/A if (timeout > 0)
so.setSoTimeout(timeout);
out.println("timeout: " + so.getSoTimeout());
testRead(so, shouldTimeout);
for (int i = 0; i < 4; i++)
testRead(so, shouldTimeout);
sc.close();
}
public static void main(String[] args) throws Exception {
test(REMOTE_HOST, 0, false);
test(REMOTE_HOST, 1000, false);
test(VERY_REMOTE_HOST, 10, true);
testRead(REMOTE_HOST, 0, false);
testRead(REMOTE_HOST, 8000, false);
testRead(VERY_REMOTE_HOST, 10, true);
}
}