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

Ping技术哪家强?

        最近想写个程序,检测一下远程主机是否在线,于是在网上找到了很多前辈的方法,现在总结一下,分享给大家,希望能够抛砖引玉:

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

函数原型:
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()方法:

函数原型:
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一次效率很低。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值