Path: blob/master/test/jdk/java/nio/channels/SocketChannel/SendUrgentData.java
41154 views
/*1* Copyright (c) 2015, 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/* @test24* @bug 807159925* @run main/othervm SendUrgentData26* @run main/othervm SendUrgentData -inline27* @summary Test sending of urgent data.28*/2930import java.io.IOException;31import java.net.InetSocketAddress;32import java.net.SocketAddress;33import java.net.SocketException;34import java.nio.ByteBuffer;35import java.nio.channels.ServerSocketChannel;36import java.nio.channels.SocketChannel;3738public class SendUrgentData {3940/**41* The arguments may be one of the following:42* <ol>43* <li>-server</li>44* <li>-client host port [-inline]</li>45* <li>[-inline]</li>46* </ol>47* The first option creates a standalone server, the second a standalone48* client, and the third a self-contained server-client pair on the49* local host.50*51* @param args52* @throws Exception53*/54public static void main(String[] args) throws Exception {5556ServerSocketChannelThread serverThread57= new ServerSocketChannelThread("SendUrgentDataServer");58serverThread.start();59boolean b = serverThread.isAlive();6061String host = null;62int port = 0;63boolean inline = false;64if (args.length > 0 && args[0].equals("-server")) {65System.out.println(serverThread.getAddress());66Thread.currentThread().suspend();67} else {68if (args.length > 0 && args[0].equals("-client")) {69host = args[1];70port = Integer.parseInt(args[2]);71if (args.length > 3) {72inline = args[2].equals("-inline");73}74} else {75host = "localhost";76port = serverThread.getAddress().getPort();77if (args.length > 0) {78inline = args[0].equals("-inline");79}80}81}8283System.out.println("OOB Inline : "+inline);8485SocketAddress sa = new InetSocketAddress(host, port);8687try (SocketChannel sc = SocketChannel.open(sa)) {88sc.configureBlocking(false);89sc.socket().setOOBInline(inline);9091sc.socket().sendUrgentData(0);92System.out.println("wrote 1 OOB byte");9394ByteBuffer bb = ByteBuffer.wrap(new byte[100 * 1000]);9596int blocked = 0;97long total = 0;9899int n;100do {101n = sc.write(bb);102if (n == 0) {103System.out.println("blocked, wrote " + total + " so far");104if (++blocked == 10) {105break;106}107Thread.sleep(100);108} else {109total += n;110bb.rewind();111}112} while (n > 0);113114long attempted = 0;115while (attempted < total) {116bb.rewind();117n = sc.write(bb);118System.out.println("wrote " + n + " normal bytes");119attempted += bb.capacity();120121String osName = System.getProperty("os.name").toLowerCase();122123try {124sc.socket().sendUrgentData(0);125} catch (IOException ex) {126if (osName.contains("linux")) {127if (!ex.getMessage().contains("Socket buffer full")) {128throw new RuntimeException("Unexpected message", ex);129}130} else if (osName.contains("os x") || osName.contains("mac")) {131if (!ex.getMessage().equals("No buffer space available")) {132throw new RuntimeException("Unexpected message", ex);133}134} else if (osName.contains("windows")) {135if (!ex.getMessage().equals("Socket buffer full")) {136throw new RuntimeException("Unexpected message", ex);137}138} else {139throw new RuntimeException("Unexpected IOException", ex);140}141}142143try {144Thread.sleep(100);145} catch (InterruptedException ex) {146// don't want to fail on this so just print trace and break147ex.printStackTrace();148break;149}150}151} finally {152serverThread.close();153}154}155156static class ServerSocketChannelThread extends Thread {157158private ServerSocketChannel ssc;159160private ServerSocketChannelThread(String name) {161super(name);162try {163ssc = ServerSocketChannel.open();164ssc.bind(new InetSocketAddress((0)));165} catch (IOException ex) {166throw new RuntimeException(ex);167}168}169170public void run() {171while (ssc.isOpen()) {172try {173Thread.sleep(100);174} catch (InterruptedException ex) {175throw new RuntimeException(ex);176}177}178try {179ssc.close();180} catch (IOException ex) {181throw new RuntimeException(ex);182}183System.out.println("ServerSocketChannelThread exiting ...");184}185186public InetSocketAddress getAddress() throws IOException {187if (ssc == null) {188throw new IllegalStateException("ServerSocketChannel not created");189}190191return (InetSocketAddress) ssc.getLocalAddress();192}193194public void close() {195try {196ssc.close();197} catch (IOException ex) {198throw new RuntimeException(ex);199}200}201}202}203204205