Path: blob/master/test/jdk/java/net/Socket/UrgentDataTest.java
41149 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 409203826* @library /test/lib27* @summary TCP Urgent data support28* @run main UrgentDataTest29* @run main/othervm -Djava.net.preferIPv4Stack=true UrgentDataTest30*/3132import java.net.*;33import java.io.*;34import jdk.test.lib.net.IPSupport;3536public class UrgentDataTest {3738Object opref;39boolean isClient, isServer;40String clHost;41ServerSocket listener;42Socket client, server;43int clPort;44InputStream clis, sis;45OutputStream clos, sos;4647static void usage () {48System.out.println (" usage: java UrgentDataTest <runs client and server together>");49System.out.println (" usage: java UrgentDataTest -server <runs server alone>");50System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+51" specified server>");52}5354public static void main (String args[]) {55IPSupport.throwSkippedExceptionIfNonOperational();5657try {58UrgentDataTest test = new UrgentDataTest ();59if (args.length == 0) {60InetAddress loopback = InetAddress.getLoopbackAddress();61test.listener = new ServerSocket ();62test.listener.bind(new InetSocketAddress(loopback, 0));63test.isClient = true;64test.isServer = true;65test.clHost = loopback.getHostAddress();66test.clPort = test.listener.getLocalPort();67test.run();68} else if (args[0].equals ("-server")) {69test.listener = new ServerSocket (0);70System.out.println ("Server listening on port " + test.listener.getLocalPort());71test.isClient = false;72test.isServer = true;73test.run();74System.out.println ("Server: Completed OK");75} else if (args[0].equals ("-client")) {76test.isClient = true;77test.isServer = false;78test.clHost = args [1];79test.clPort = Integer.parseInt (args[2]);80test.run();81System.out.println ("Client: Completed OK");82} else {83usage ();84}85}86catch (ArrayIndexOutOfBoundsException e) {87usage();88}89catch (NumberFormatException e) {90usage();91}92catch (Exception e) {93e.printStackTrace ();94throw new RuntimeException ("Exception caught");95}96}9798public void run () throws Exception {99try {100if (isClient) {101client = new Socket (clHost, clPort);102clis = client.getInputStream();103clos = client.getOutputStream();104client.setOOBInline (true);105if (client.getOOBInline() != true) {106throw new RuntimeException ("Setting OOBINLINE failed");107}108}109if (isServer) {110server = listener.accept ();111sis = server.getInputStream();112sos = server.getOutputStream();113}114if (isClient) {115clos.write ("Hello".getBytes ());116client.sendUrgentData (100);117clos.write ("world".getBytes ());118}119// read Hello world from server (during which oob byte must have been dropped)120String s = "Helloworld";121if (isServer) {122for (int y=0; y<s.length(); y++) {123int c = sis.read ();124if (c != (int)s.charAt (y)) {125throw new RuntimeException ("Unexpected character read");126}127}128// Do the same from server to client129sos.write ("Hello".getBytes ());130server.sendUrgentData (101);131sos.write ("World".getBytes ());132}133if (isClient) {134// read Hello world from client (during which oob byte must have been read)135s="Hello";136for (int y=0; y<s.length(); y++) {137int c = clis.read ();138if (c != (int)s.charAt (y)) {139throw new RuntimeException ("Unexpected character read");140}141}142if (clis.read() != 101) {143throw new RuntimeException ("OOB byte not received");144}145s="World";146for (int y=0; y<s.length(); y++) {147int c = clis.read ();148if (c != (int)s.charAt (y)) {149throw new RuntimeException ("Unexpected character read");150}151}152}153} finally {154if (listener != null) listener.close();155if (client != null) client.close ();156if (server != null) server.close ();157}158}159}160161162