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/*
0N/A * @test
0N/A * @bug 4092038
0N/A * @summary TCP Urgent data support
0N/A *
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class UrgentDataTest {
0N/A
0N/A Object opref;
0N/A boolean isClient, isServer;
0N/A String clHost;
0N/A ServerSocket listener;
0N/A Socket client, server;
0N/A int clPort;
0N/A InputStream clis, sis;
0N/A OutputStream clos, sos;
0N/A
0N/A static void usage () {
0N/A System.out.println (" usage: java UrgentDataTest <runs client and server together>");
0N/A System.out.println (" usage: java UrgentDataTest -server <runs server alone>");
0N/A System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+
0N/A " specified server>");
0N/A }
0N/A
0N/A public static void main (String args[]) {
0N/A try {
0N/A UrgentDataTest test = new UrgentDataTest ();
0N/A if (args.length == 0) {
0N/A test.listener = new ServerSocket (0);
0N/A test.isClient = true;
0N/A test.isServer = true;
0N/A test.clHost = "127.0.0.1";
0N/A test.clPort = test.listener.getLocalPort();
0N/A test.run();
0N/A } else if (args[0].equals ("-server")) {
0N/A test.listener = new ServerSocket (0);
0N/A System.out.println ("Server listening on port " + test.listener.getLocalPort());
0N/A test.isClient = false;
0N/A test.isServer = true;
0N/A test.run();
0N/A System.out.println ("Server: Completed OK");
0N/A } else if (args[0].equals ("-client")) {
0N/A test.isClient = true;
0N/A test.isServer = false;
0N/A test.clHost = args [1];
0N/A test.clPort = Integer.parseInt (args[2]);
0N/A test.run();
0N/A System.out.println ("Client: Completed OK");
0N/A } else {
0N/A usage ();
0N/A }
0N/A }
0N/A catch (ArrayIndexOutOfBoundsException e) {
0N/A usage();
0N/A }
0N/A catch (NumberFormatException e) {
0N/A usage();
0N/A }
0N/A catch (Exception e) {
0N/A e.printStackTrace ();
0N/A throw new RuntimeException ("Exception caught");
0N/A }
0N/A }
0N/A
0N/A public void run () throws Exception {
2612N/A try {
2612N/A if (isClient) {
2612N/A client = new Socket (clHost, clPort);
2612N/A clis = client.getInputStream();
2612N/A clos = client.getOutputStream();
2612N/A client.setOOBInline (true);
2612N/A if (client.getOOBInline() != true) {
2612N/A throw new RuntimeException ("Setting OOBINLINE failed");
0N/A }
0N/A }
2612N/A if (isServer) {
2612N/A server = listener.accept ();
2612N/A sis = server.getInputStream();
2612N/A sos = server.getOutputStream();
2612N/A }
2612N/A if (isClient) {
2612N/A clos.write ("Hello".getBytes ());
2612N/A client.sendUrgentData (100);
2612N/A clos.write ("world".getBytes ());
2612N/A }
2612N/A // read Hello world from server (during which oob byte must have been dropped)
2612N/A String s = "Helloworld";
2612N/A if (isServer) {
2612N/A for (int y=0; y<s.length(); y++) {
2612N/A int c = sis.read ();
2612N/A if (c != (int)s.charAt (y)) {
2612N/A throw new RuntimeException ("Unexpected character read");
2612N/A }
2612N/A }
2612N/A // Do the same from server to client
2612N/A sos.write ("Hello".getBytes ());
2612N/A server.sendUrgentData (101);
2612N/A sos.write ("World".getBytes ());
2612N/A }
2612N/A if (isClient) {
2612N/A // read Hello world from client (during which oob byte must have been read)
2612N/A s="Hello";
2612N/A for (int y=0; y<s.length(); y++) {
2612N/A int c = clis.read ();
2612N/A if (c != (int)s.charAt (y)) {
2612N/A throw new RuntimeException ("Unexpected character read");
2612N/A }
2612N/A }
2612N/A if (clis.read() != 101) {
2612N/A throw new RuntimeException ("OOB byte not received");
2612N/A }
2612N/A s="World";
2612N/A for (int y=0; y<s.length(); y++) {
2612N/A int c = clis.read ();
2612N/A if (c != (int)s.charAt (y)) {
2612N/A throw new RuntimeException ("Unexpected character read");
2612N/A }
0N/A }
0N/A }
2612N/A } finally {
2612N/A if (listener != null) listener.close();
2612N/A if (client != null) client.close ();
2612N/A if (server != null) server.close ();
0N/A }
0N/A }
0N/A}