Path: blob/master/test/jdk/java/net/Socket/setReuseAddress/Restart.java
41153 views
/*1* Copyright (c) 2001, 2019, 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*/2223/*24* @test25* @bug 447637826* @library /test/lib27* @summary Check that SO_REUSEADDR allows a server to restart28* after a crash.29* @run main Restart30* @run main/othervm -Dsun.net.useExclusiveBind Restart31* @run main/othervm -Dsun.net.useExclusiveBind=true Restart32* @run main/othervm -Djava.net.preferIPv4Stack=true Restart33* @run main/othervm -Dsun.net.useExclusiveBind34* -Djava.net.preferIPv4Stack=true Restart35* @run main/othervm -Dsun.net.useExclusiveBind=true36* -Djava.net.preferIPv4Stack=true Restart37*/38import java.net.*;39import jdk.test.lib.net.IPSupport;4041public class Restart {4243/*44* Test that a server can bind to the same port after45* a crash -- ie: while previous connection still in46* TIME_WAIT state we should be able to re-bind if47* SO_REUSEADDR is enabled.48*/4950public static void main(String args[]) throws Exception {51IPSupport.throwSkippedExceptionIfNonOperational();5253InetAddress localHost = InetAddress.getLocalHost();54ServerSocket ss = new ServerSocket(0, 0, localHost);55Socket s1 = null, s2 = null;56try {57int port = ss.getLocalPort();5859s1 = new Socket(localHost, port);60s2 = ss.accept();6162// close server socket and the accepted connection63ss.close();64s2.close();6566ss = new ServerSocket();67ss.bind( new InetSocketAddress(localHost, port) );68ss.close();6970// close the client socket71s1.close();72} catch (BindException be) {73if (System.getProperty("sun.net.useExclusiveBind") != null) {74// exclusive bind, expected exception75} else {76throw be;77}78} finally {79if (ss != null) ss.close();80if (s1 != null) s1.close();81if (s2 != null) s2.close();82}83}84}858687