jdk/test/java/net/Socket/UrgentDataTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 6115 7c523cf2bc8a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4092038
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary TCP Urgent data support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class UrgentDataTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    Object opref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    boolean isClient, isServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    String  clHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    ServerSocket listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    Socket client, server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int  clPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    InputStream clis, sis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    OutputStream clos, sos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static void usage () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.out.println ("   usage: java UrgentDataTest <runs client and server together>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        System.out.println ("   usage: java UrgentDataTest -server <runs server alone>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        System.out.println ("   usage: java UrgentDataTest -client host port <runs client and connects to"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            " specified server>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static void main (String args[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            UrgentDataTest test = new UrgentDataTest ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (args.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                test.listener = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                test.isClient = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                test.isServer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                test.clHost = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                test.clPort = test.listener.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } else if (args[0].equals ("-server")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                test.listener = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                System.out.println ("Server listening on port " + test.listener.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                test.isClient = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                test.isServer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                System.out.println ("Server: Completed OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            } else if (args[0].equals ("-client")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                test.isClient = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                test.isServer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                test.clHost = args [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                test.clPort = Integer.parseInt (args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                System.out.println ("Client: Completed OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                usage ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            e.printStackTrace ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException ("Exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public void run () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            client = new Socket (clHost, clPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            clis = client.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            clos = client.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            client.setOOBInline (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (client.getOOBInline() != true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                throw new RuntimeException ("Setting OOBINLINE failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (isServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            server = listener.accept ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            sis = server.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            sos = server.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            clos.write ("Hello".getBytes ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            client.sendUrgentData (100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            clos.write ("world".getBytes ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // read Hello world from server (during which oob byte must have been dropped)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String s = "Helloworld";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (isServer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            for (int y=0; y<s.length(); y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                int c = sis.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (c != (int)s.charAt (y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    throw new RuntimeException ("Unexpected character read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Do the same from server to client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            sos.write ("Hello".getBytes ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            server.sendUrgentData (101);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            sos.write ("World".getBytes ());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (isClient) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            // read Hello world from client (during which oob byte must have been read)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            s="Hello";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            for (int y=0; y<s.length(); y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                int c = clis.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (c != (int)s.charAt (y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    throw new RuntimeException ("Unexpected character read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (clis.read() != 101) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                throw new RuntimeException ("OOB byte not received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            s="World";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            for (int y=0; y<s.length(); y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                int c = clis.read ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                if (c != (int)s.charAt (y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    throw new RuntimeException ("Unexpected character read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (isClient)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            client.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (isServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            server.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}