jdk/test/java/nio/channels/SocketChannel/AdaptSocket.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
child 13028 f72c7e618ed0
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Unit test for socket-channel adaptors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.charset.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class AdaptSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static java.io.PrintStream out = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static final int ECHO_PORT = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static final int DAYTIME_PORT = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static final String REMOTE_HOST = TestUtil.HOST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final String VERY_REMOTE_HOST = TestUtil.FAR_HOST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static void test(String hn, int timeout, boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        InetSocketAddress isa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            = new InetSocketAddress(InetAddress.getByName(hn),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                    DAYTIME_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Socket so = sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        out.println("opened: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        out.println("        " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        //out.println("opts:   " + sc.options());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        so.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        //so.setTrafficClass(SocketOpts.IP.TOS_THROUGHPUT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        so.setKeepAlive(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        so.setSoLinger(true, 42);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        so.setOOBInline(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        so.setReceiveBufferSize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        so.setSendBufferSize(512);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        //out.println("        " + sc.options());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (timeout == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            so.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                so.connect(isa, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    out.println("Connection timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        out.println("connected: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        out.println("           " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] bb = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int n = so.getInputStream().read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String s = new String(bb, 0, n - 2, "US-ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        out.println(isa + " says: \"" + s + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        so.shutdownInput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        out.println("ishut: " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        so.shutdownOutput();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        out.println("oshut: " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        so.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        out.println("closed: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        out.println("        " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    static String dataString = "foo\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static void testRead(Socket so, boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        String data = "foo\r\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        so.getOutputStream().write(dataString.getBytes("US-ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        InputStream is = so.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            byte[] b = new byte[100];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int n = is.read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (n != 5)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                throw new Exception("Incorrect number of bytes read: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (!dataString.equals(new String(b, 0, n, "US-ASCII")))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new Exception("Incorrect data read: " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        } catch (SocketTimeoutException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (shouldTimeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                out.println("Read timed out, as expected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static void testRead(String hn, int timeout, boolean shouldTimeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throws Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        InetSocketAddress isa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            = new InetSocketAddress(InetAddress.getByName(hn), ECHO_PORT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        sc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Socket so = sc.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        out.println("connected: " + so);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        out.println("           " + sc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (timeout > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            so.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        out.println("timeout: " + so.getSoTimeout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        testRead(so, shouldTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (!TestUtil.onME())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            for (int i = 0; i < 4; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                testRead(so, shouldTimeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        test(REMOTE_HOST, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        test(REMOTE_HOST, 1000, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        test(VERY_REMOTE_HOST, 10, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        testRead(REMOTE_HOST, 0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        testRead(REMOTE_HOST, 8000, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        testRead(VERY_REMOTE_HOST, 10, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}