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