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