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  * @test
25  * @bug 4408755
26  * @summary This tests whether it's possible to get some informations
27  *          out of a closed socket. This is for backward compatibility
28  *          purposes.
29  * @run main TestClose
30  * @run main/othervm -Djava.net.preferIPv4Stack=true TestClose
31  */
32 
33 import java.net.*;
34 
35 public class TestClose {
36 
main(String[] args)37     public static void main(String[] args) throws Exception {
38         ServerSocket ss;
39         Socket s;
40         InetAddress ad1, ad2;
41         int port1, port2, serverport;
42 
43         InetAddress loopback = InetAddress.getLoopbackAddress();
44         ss = new ServerSocket();
45         ss.bind(new InetSocketAddress(loopback, 0));
46         serverport = ss.getLocalPort();
47         s = new Socket(loopback, serverport);
48         s.close();
49         ss.close();
50         ad1 = ss.getInetAddress();
51         if (ad1 == null)
52             throw new RuntimeException("ServerSocket.getInetAddress() returned null");
53         port1 = ss.getLocalPort();
54         if (port1 != serverport)
55             throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");
56         ad2 = s.getInetAddress();
57         if (ad2 == null)
58             throw new RuntimeException("Socket.getInetAddress() returned null");
59         port2 = s.getPort();
60         if (port2 != serverport)
61             throw new RuntimeException("Socket.getPort() returned wrong value");
62         ad2 = s.getLocalAddress();
63         if (ad2 == null)
64             throw new RuntimeException("Socket.getLocalAddress() returned null");
65         port2 = s.getLocalPort();
66         if (port2 == -1)
67             throw new RuntimeException("Socket.getLocalPort returned -1");
68     }
69 }
70