test/jdk/java/net/Socket/NullHost.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 47216 71c04702a3d5
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    34     class Server extends Thread {
    34     class Server extends Thread {
    35         private ServerSocket svr;
    35         private ServerSocket svr;
    36 
    36 
    37         public Server() throws IOException {
    37         public Server() throws IOException {
    38             svr = new ServerSocket();
    38             svr = new ServerSocket();
    39             svr.bind(new InetSocketAddress(0));
    39             // The client side calls Socket((String) null, ...) which
       
    40             // resolves to InetAddress.getByName((String)null) which in
       
    41             // turns will resolve to the loopback address
       
    42             svr.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
    40         }
    43         }
    41 
    44 
    42         public int getPort() {
    45         public int getPort() {
    43             return svr.getLocalPort();
    46             return svr.getLocalPort();
    44         }
    47         }
    45 
    48 
       
    49         volatile boolean done;
    46         public void shutdown() {
    50         public void shutdown() {
    47             try {
    51             try {
       
    52                 done = true;
    48                 svr.close();
    53                 svr.close();
    49             } catch (IOException e) {
    54             } catch (IOException e) {
    50             }
    55             }
    51         }
    56         }
    52 
    57 
    53         public void run() {
    58         public void run() {
    54             Socket s;
    59             Socket s;
    55             try {
    60             try {
    56                 while (true) {
    61                 while (!done) {
    57                     s = svr.accept();
    62                     s = svr.accept();
    58                     s.close();
    63                     s.close();
    59                 }
    64                 }
    60             } catch (IOException e) {
    65             } catch (IOException e) {
       
    66                 if (!done) e.printStackTrace();
    61             }
    67             }
    62         }
    68         }
    63     }
    69     }
    64 
    70 
    65     public static void main(String[] args) throws IOException {
    71     public static void main(String[] args) throws IOException {
    69     public NullHost () throws IOException {
    75     public NullHost () throws IOException {
    70         Server s = new Server();
    76         Server s = new Server();
    71         int port = s.getPort();
    77         int port = s.getPort();
    72         s.start();
    78         s.start();
    73         try {
    79         try {
    74             Socket sock = new Socket((String)null, port);
    80             try (var sock = new Socket((String)null, port)) {}
    75             sock.close();
    81             try (var sock = new Socket((String)null, port, true)) {}
    76             sock = new Socket((String)null, port, true);
    82             try (var sock = new Socket((String)null, port, null, 0)) {}
    77             sock.close();
       
    78             sock = new Socket((String)null, port, null, 0);
       
    79             sock.close();
       
    80 
       
    81         } catch (NullPointerException e) {
    83         } catch (NullPointerException e) {
    82             throw new RuntimeException("Got a NPE");
    84             throw new RuntimeException("Got a NPE");
    83         } finally {
    85         } finally {
    84             s.shutdown();
    86             s.shutdown();
    85         }
    87         }