`
iaiai
  • 浏览: 2144429 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

[java] 判断两台机器之间网络是否可达

 
阅读更多
Java类库判断两台机器之间网络是否可达,常用ping方法来实现。

package com.iaiai.test;

import java.net.InetAddress;

/**
 * 
 * <br/>
 * Title: Test.java<br/>
 * E-Mail: 176291935@qq.com<br/>
 * QQ: 176291935<br/>
 * Http: iaiai.iteye.com<br/>
 * Create time: 2013-1-29 11:19:55<br/>
 * <br/>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class Test {

	public static void main(String[] args) {
		String IP = "1.1.1.1";
		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;
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics