Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/setReuseAddress/Restart.java
38828 views
/*1* Copyright (c) 2001, 2012, 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* @summary Check that SO_REUSEADDR allows a server to restart27* after a crash.28* @run main Restart29* @run main/othervm -Dsun.net.useExclusiveBind Restart30*/31import java.net.*;3233public class Restart {3435/*36* Test that a server can bind to the same port after37* a crash -- ie: while previous connection still in38* TIME_WAIT state we should be able to re-bind if39* SO_REUSEADDR is enabled.40*/4142public static void main(String args[]) throws Exception {43ServerSocket ss = new ServerSocket(0);44Socket s1 = null, s2 = null;45try {46int port = ss.getLocalPort();4748s1 = new Socket(InetAddress.getLocalHost(), port);49s2 = ss.accept();5051// close server socket and the accepted connection52ss.close();53s2.close();5455ss = new ServerSocket();56ss.bind( new InetSocketAddress(port) );57ss.close();5859// close the client socket60s1.close();61} catch (BindException be) {62if (System.getProperty("sun.net.useExclusiveBind") != null) {63// exclusive bind, expected exception64} else {65throw be;66}67} finally {68if (ss != null) ss.close();69if (s1 != null) s1.close();70if (s2 != null) s2.close();71}72}73}747576