Path: blob/master/test/jdk/java/net/Inet4Address/PingThis.java
41149 views
/*1* Copyright (c) 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* Portions Copyright (c) 2012 IBM Corporation25*/2627/* @test28* @bug 7163874 813301529* @library /test/lib30* @summary InetAddress.isReachable is returning false31* for InetAdress 0.0.0.0 and ::032* @run main PingThis33* @run main/othervm -Djava.net.preferIPv4Stack=true PingThis34*/3536import java.net.Inet6Address;37import java.net.InetAddress;38import java.net.NetworkInterface;39import java.util.ArrayList;40import java.util.Collections;41import java.util.Iterator;42import java.util.List;43import jdk.test.lib.net.IPSupport;4445public class PingThis {46public static void main(String args[]) throws Exception {47if (System.getProperty("os.name").startsWith("Windows")) {48return;49}50IPSupport.throwSkippedExceptionIfNonOperational();5152List<String> addrs = new ArrayList<String>();5354if (IPSupport.hasIPv4()) {55addrs.add("0.0.0.0");56}57if (IPSupport.hasIPv6()) {58addrs.add("::0");59}6061for (String addr : addrs) {62InetAddress inetAddress = InetAddress.getByName(addr);63System.out.println("The target ip is "64+ inetAddress.getHostAddress());65boolean isReachable = inetAddress.isReachable(3000);66System.out.println("the target is reachable: " + isReachable);67if (isReachable) {68System.out.println("Test passed ");69} else {70System.out.println("Test failed ");71throw new Exception("address " + inetAddress.getHostAddress()72+ " can not be reachable!");73}74}75}76}777879