Java学习篇之--检测主机是否在线Ping

Ping技术哪家强?

        最近想写个程序,检测一下远程主机是否在线,因而在网上找到了不少前辈的方法,如今总结一下,分享给你们,但愿可以抛砖引玉:java

        方法一:利用java.net包中的类InetAddress中的getByAddress()方法:app

函数原型:
public static InetAddress getByAddress(byte[] addr)
                                throws UnknownHostException


public boolean isReachable(int timeout)
                    throws IOException

注释: Test whether that address is reachable.

       

源码:
package com.ryze.ping.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class PingTest {

	public static void main(String[] args) throws UnknownHostException, IOException {
		InetAddress inet;
		inet = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
		System.out.println("Sending Ping Request to " + inet);
		System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");

		inet = InetAddress.getByAddress(new byte[] { (byte) 220, (byte) 181, 112, (byte)244 });
		System.out.println("Sending Ping Request to " + inet);
		System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
	}
}

运行结果:
Sending Ping Request to /127.0.0.1
Host is reachable
Sending Ping Request to /220.181.112.244
Host is NOT reachable

        方法二:利用java.net包中的类InetAddress中的getByName()方法:函数

函数原型:
public static InetAddress getByName(String host)
                             throws UnknownHostException


源码:
package com.ryze.ping.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class PingTest {

	public static void main(String[] args) throws UnknownHostException, IOException {
	    String ipAddress = "127.0.0.1";
	    InetAddress inet = InetAddress.getByName(ipAddress);

	    System.out.println("Sending Ping Request to " + ipAddress);
	    System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");

	    ipAddress = "220.181.112.244";
	    inet = InetAddress.getByName(ipAddress);

	    System.out.println("Sending Ping Request to " + ipAddress);
	    System.out.println(inet.isReachable(5000) ? "Host is reachable" : "Host is NOT reachable");
	}
}

运行结果:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 220.181.112.244
Host is NOT reachable

        方法三:利用java.lang包中的类Runtime中的getRuntime()方法:this

函数原型:
public static Runtime getRuntime()

注释: returns the runtime object associated with the current Java application.


函数原型:
public Process exec(String command)
             throws IOException

注释: Executes the specified string command in a separate process.


函数原型:
public abstract int waitFor()
                     throws InterruptedException

注释: the exit value of the subprocess represented by this  Process  object. By convention, the value  0  indicates normal termination.


源码:
package com.ryze.ping.test;

public class PingTest {

	public static void main(String[] args) {
			    
	    boolean reachable = false;
	    Process process;
	    try {
	    	String ipAddress = "127.0.0.1";
			process = Runtime.getRuntime().exec("ping " + ipAddress);
			int returnVal = process.waitFor();
			reachable = returnVal == 0?true:false;
			System.out.println("Sending Ping Request to " + ipAddress);
			System.out.println(reachable ? "Host is reachable" : "Host is NOT reachable");
			
			ipAddress = "220.181.112.244";
			process = Runtime.getRuntime().exec("ping " + ipAddress);
			returnVal = process.waitFor();
			reachable = returnVal == 0?true:false;
			System.out.println("Sending Ping Request to " + ipAddress);
			System.out.println(reachable ? "Host is reachable" : "Host is NOT reachable");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}


运行结果:
Sending Ping Request to 127.0.0.1
Host is reachable
Sending Ping Request to 220.181.112.244
Host is reachable
        小结:经过google搜索加上本身亲自实验,发现前两种方法只能ping通局域网内的Ip,对于外网的ip通常ping不通,第三种方法个人理解是至关于在本地DOS环境下进行ping,能ping通外网,可是每ping一次效率很低。