Java類庫判斷兩臺機器之間網絡是否可達

yefx 9年前發布 | 2K 次閱讀 Java
Java類庫判斷兩臺機器之間網絡是否可達,常用ping方法來實現。
import java.net.InetAddress;

public class Test {

    public static void main(String[] args) {
        String IP = "10.1.11.225";
        if (Test.ping(IP))
            System.out.println("SUCCESS - ping " + IP + " with no interface specified");
        else
            System.out.println("FAILURE - ping " + IP + " with no interface specified");

    }

    /**
     * 
     * @param host 主機地址
     * 
     * @return boolean
     * */
    public static boolean ping(String host) {
        String $host = host;
        try {
            InetAddress address = null;
            if ($host != null && $host.trim().length() > 0) {
                address = InetAddress.getByName($host);
            }
            if (address != null) {

            } else {
                System.out.println($host + " is unrecongized");
            }
            if (address.isReachable(5000))
                return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

 本文由用戶 yefx 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!