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一次效率很低。