1 /*
2  * Copyright (c) 2001, 2018, 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         ss = new ServerSocket(0);
44         serverport = ss.getLocalPort();
45         s = new Socket("localhost", serverport);
46         s.close();
47         ss.close();
48         ad1 = ss.getInetAddress();
49         if (ad1 == null)
50             throw new RuntimeException("ServerSocket.getInetAddress() returned null");
51         port1 = ss.getLocalPort();
52         if (port1 != serverport)
53             throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");
54         ad2 = s.getInetAddress();
55         if (ad2 == null)
56             throw new RuntimeException("Socket.getInetAddress() returned null");
57         port2 = s.getPort();
58         if (port2 != serverport)
59             throw new RuntimeException("Socket.getPort() returned wrong value");
60         ad2 = s.getLocalAddress();
61         if (ad2 == null)
62             throw new RuntimeException("Socket.getLocalAddress() returned null");
63         port2 = s.getLocalPort();
64         if (port2 == -1)
65             throw new RuntimeException("Socket.getLocalPort returned -1");
66     }
67 }
68