Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/TestClose.java
38812 views
/*1* Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24*25* @bug 440875526*27* @summary This tests wether it's possible to get some informations28* out of a closed socket. This is for backward compatibility purposes.29*/3031import java.net.*;3233public class TestClose {3435public static void main(String[] args) throws Exception {36ServerSocket ss;37Socket s;38InetAddress ad1, ad2;39int port1, port2, serverport;4041ss = new ServerSocket(0);42serverport = ss.getLocalPort();43s = new Socket("localhost", serverport);44s.close();45ss.close();46ad1 = ss.getInetAddress();47if (ad1 == null)48throw new RuntimeException("ServerSocket.getInetAddress() returned null");49port1 = ss.getLocalPort();50if (port1 != serverport)51throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");52ad2 = s.getInetAddress();53if (ad2 == null)54throw new RuntimeException("Socket.getInetAddress() returned null");55port2 = s.getPort();56if (port2 != serverport)57throw new RuntimeException("Socket.getPort() returned wrong value");58ad2 = s.getLocalAddress();59if (ad2 == null)60throw new RuntimeException("Socket.getLocalAddress() returned null");61port2 = s.getLocalPort();62if (port2 == -1)63throw new RuntimeException("Socket.getLocalPort returned -1");64}65}666768