test/jdk/java/nio/channels/spi/SelectorProvider/inheritedChannel/Launcher.java
changeset 59305 219ec11136d9
parent 58313 8a2bc12d2a9a
equal deleted inserted replaced
59304:643d9cf3d8fc 59305:219ec11136d9
       
     1 
     1 /*
     2 /*
     2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * This code is free software; you can redistribute it and/or modify it
    89      * The launched process will inherit a connected TCP socket. The remote endpoint
    90      * The launched process will inherit a connected TCP socket. The remote endpoint
    90      * will be the SocketChannel returned by this method.
    91      * will be the SocketChannel returned by this method.
    91      */
    92      */
    92     public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
    93     public static SocketChannel launchWithSocketChannel(String className, String options[], String args[]) throws IOException {
    93         ServerSocketChannel ssc = ServerSocketChannel.open();
    94         ServerSocketChannel ssc = ServerSocketChannel.open();
    94         ssc.socket().bind(new InetSocketAddress(0));
    95         ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
    95         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
    96         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(),
    96                                                       ssc.socket().getLocalPort());
    97                                                       ssc.socket().getLocalPort());
    97         SocketChannel sc1 = SocketChannel.open(isa);
    98         SocketChannel sc1 = SocketChannel.open(isa);
    98         SocketChannel sc2 = ssc.accept();
    99         SocketChannel sc2 = ssc.accept();
    99         launch(className, options, args, Util.getFD(sc2));
   100         launch(className, options, args, Util.getFD(sc2));
   118      */
   119      */
   119     public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
   120     public static SocketChannel launchWithServerSocketChannel(String className, String options[], String args[])
   120         throws IOException
   121         throws IOException
   121     {
   122     {
   122         ServerSocketChannel ssc = ServerSocketChannel.open();
   123         ServerSocketChannel ssc = ServerSocketChannel.open();
   123         ssc.socket().bind(new InetSocketAddress(0));
   124         ssc.socket().bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
   124         int port = ssc.socket().getLocalPort();
   125         int port = ssc.socket().getLocalPort();
   125         launch(className, options, args, Util.getFD(ssc));
   126         launch(className, options, args, Util.getFD(ssc));
   126         ssc.close();
   127         ssc.close();
   127         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
   128         InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost(), port);
   128         return SocketChannel.open(isa);
   129         return SocketChannel.open(isa);
   145      * sent to the service.
   146      * sent to the service.
   146      */
   147      */
   147     public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
   148     public static DatagramChannel launchWithDatagramChannel(String className, String options[], String args[])
   148         throws IOException
   149         throws IOException
   149     {
   150     {
       
   151         InetAddress address = InetAddress.getLocalHost();
       
   152         if (address.isLoopbackAddress()) {
       
   153             address = InetAddress.getLoopbackAddress();
       
   154         }
   150         DatagramChannel dc = DatagramChannel.open();
   155         DatagramChannel dc = DatagramChannel.open();
   151         dc.socket().bind(new InetSocketAddress(0));
   156         dc.socket().bind(new InetSocketAddress(address, 0));
   152 
   157 
   153         int port = dc.socket().getLocalPort();
   158         int port = dc.socket().getLocalPort();
   154         launch(className, options, args, Util.getFD(dc));
   159         launch(className, options, args, Util.getFD(dc));
   155         dc.close();
   160         dc.close();
   156 
   161 
   157         dc = DatagramChannel.open();
   162         dc = DatagramChannel.open();
   158         InetAddress address = InetAddress.getLocalHost();
       
   159         if (address.isLoopbackAddress()) {
       
   160             address = InetAddress.getLoopbackAddress();
       
   161         }
       
   162         InetSocketAddress isa = new InetSocketAddress(address, port);
   163         InetSocketAddress isa = new InetSocketAddress(address, port);
   163 
   164 
   164         dc.connect(isa);
   165         dc.connect(isa);
   165         return dc;
   166         return dc;
   166     }
   167     }