Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/UrgentDataTest.java
38812 views
/*1* Copyright (c) 2001, 2010, 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* @summary TCP Urgent data support27*28*/2930import java.net.*;31import java.io.*;3233public class UrgentDataTest {3435Object opref;36boolean isClient, isServer;37String clHost;38ServerSocket listener;39Socket client, server;40int clPort;41InputStream clis, sis;42OutputStream clos, sos;4344static void usage () {45System.out.println (" usage: java UrgentDataTest <runs client and server together>");46System.out.println (" usage: java UrgentDataTest -server <runs server alone>");47System.out.println (" usage: java UrgentDataTest -client host port <runs client and connects to"+48" specified server>");49}5051public static void main (String args[]) {52try {53UrgentDataTest test = new UrgentDataTest ();54if (args.length == 0) {55test.listener = new ServerSocket (0);56test.isClient = true;57test.isServer = true;58test.clHost = "127.0.0.1";59test.clPort = test.listener.getLocalPort();60test.run();61} else if (args[0].equals ("-server")) {62test.listener = new ServerSocket (0);63System.out.println ("Server listening on port " + test.listener.getLocalPort());64test.isClient = false;65test.isServer = true;66test.run();67System.out.println ("Server: Completed OK");68} else if (args[0].equals ("-client")) {69test.isClient = true;70test.isServer = false;71test.clHost = args [1];72test.clPort = Integer.parseInt (args[2]);73test.run();74System.out.println ("Client: Completed OK");75} else {76usage ();77}78}79catch (ArrayIndexOutOfBoundsException e) {80usage();81}82catch (NumberFormatException e) {83usage();84}85catch (Exception e) {86e.printStackTrace ();87throw new RuntimeException ("Exception caught");88}89}9091public void run () throws Exception {92try {93if (isClient) {94client = new Socket (clHost, clPort);95clis = client.getInputStream();96clos = client.getOutputStream();97client.setOOBInline (true);98if (client.getOOBInline() != true) {99throw new RuntimeException ("Setting OOBINLINE failed");100}101}102if (isServer) {103server = listener.accept ();104sis = server.getInputStream();105sos = server.getOutputStream();106}107if (isClient) {108clos.write ("Hello".getBytes ());109client.sendUrgentData (100);110clos.write ("world".getBytes ());111}112// read Hello world from server (during which oob byte must have been dropped)113String s = "Helloworld";114if (isServer) {115for (int y=0; y<s.length(); y++) {116int c = sis.read ();117if (c != (int)s.charAt (y)) {118throw new RuntimeException ("Unexpected character read");119}120}121// Do the same from server to client122sos.write ("Hello".getBytes ());123server.sendUrgentData (101);124sos.write ("World".getBytes ());125}126if (isClient) {127// read Hello world from client (during which oob byte must have been read)128s="Hello";129for (int y=0; y<s.length(); y++) {130int c = clis.read ();131if (c != (int)s.charAt (y)) {132throw new RuntimeException ("Unexpected character read");133}134}135if (clis.read() != 101) {136throw new RuntimeException ("OOB byte not received");137}138s="World";139for (int y=0; y<s.length(); y++) {140int c = clis.read ();141if (c != (int)s.charAt (y)) {142throw new RuntimeException ("Unexpected character read");143}144}145}146} finally {147if (listener != null) listener.close();148if (client != null) client.close ();149if (server != null) server.close ();150}151}152}153154155