Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/GetLocalAddress.java
38812 views
/*1* Copyright (c) 1998, 2013, 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 4106601 8026245 807142426* @run main/othervm GetLocalAddress27* @run main/othervm -Djava.net.preferIPv4Stack=true GetLocalAddress28* @run main/othervm -Djava.net.preferIPv6Addresses=true GetLocalAddress29* @summary Test the java.net.socket.GetLocalAddress method30*31*/3233import java.net.*;3435public class GetLocalAddress implements Runnable {36static ServerSocket ss;37static InetAddress addr;38static int port;3940public static void main(String args[]) throws Exception {41testBindNull();4243boolean error = true;44int linger = 65546;45int value = 0;46addr = InetAddress.getLocalHost();47ss = new ServerSocket(0);48port = ss.getLocalPort();4950Thread t = new Thread(new GetLocalAddress());51t.start();52Socket soc = ss.accept();5354if(addr.equals(soc.getLocalAddress())) {55error = false;56}57if (error)58throw new RuntimeException("Socket.GetLocalAddress failed.");59soc.close();60}6162public void run() {63try {64Socket s = new Socket(addr, port);65} catch (Exception e) {66e.printStackTrace();67}68}6970static void testBindNull() throws Exception {71try (Socket soc = new Socket()) {72soc.bind(null);73if (!soc.isBound())74throw new RuntimeException(75"should be bound after bind(null)");76if (soc.getLocalPort() <= 0)77throw new RuntimeException(78"bind(null) failed, local port: " + soc.getLocalPort());79if (soc.getLocalAddress() == null)80throw new RuntimeException(81"bind(null) failed, local address is null");82}83}8485}868788