JAVA中Ping IP地址的两种方法
2016-01-19 16:11
483 查看
今天进行IP巡检时用到了PINGIP地址,以获取该IP址是否可用,查了一些文档后,发现了两种PING的方法,但试用后,还是发现第一种比较好用,如果在局域网内,当然,第二种更有效率: 上面上代码
// 方法一 最常用的 PING 方法
Runtime runtime =Runtime.getRuntime(); // 获取当前程序的运行进对象
Process process = null; //声明处理类对象
String line = null; //返回行信息
InputStream is = null; //输入流
InputStreamReader isr = null;// 字节流
BufferedReader br = null;
String ip = "www.baidu.com";
boolean res = false;// 结果
try {
process =runtime.exec("ping " + ip); // PING
is =process.getInputStream(); // 实例化输入流
isr = newInputStreamReader(is);// 把输入流转换成字节流
br = newBufferedReader(isr);// 从字节中读取文本
while ((line= br.readLine()) != null) {
if(line.contains("TTL")) {
res= true;
break;
}
}
is.close();
isr.close();
br.close();
if (res){
System.out.println("ping通 ...");
} else{
System.out.println("ping不通...");
}
} catch (IOException e) {
System.out.println(e);
runtime.exit(1);
}
//方法二 下面代码为 JDK1.5PING的新方法但不能用,因为 该PING请求端口为7 而大型网站会关闭不需要的端口防止入侵
InetAddress address;
try {
address =InetAddress.getByName("www.weibo.com");
System.out.println("Name:" + address.getHostName());
System.out.println("Addr:" + address.getHostAddress());
System.out.println("Reach:" + address.isReachable(3000)); //是否能通信 返回true或false
System.out.println(address.toString());
} catch (Exception e) {
e.printStackTrace();
}
// 方法一 最常用的 PING 方法
Runtime runtime =Runtime.getRuntime(); // 获取当前程序的运行进对象
Process process = null; //声明处理类对象
String line = null; //返回行信息
InputStream is = null; //输入流
InputStreamReader isr = null;// 字节流
BufferedReader br = null;
String ip = "www.baidu.com";
boolean res = false;// 结果
try {
process =runtime.exec("ping " + ip); // PING
is =process.getInputStream(); // 实例化输入流
isr = newInputStreamReader(is);// 把输入流转换成字节流
br = newBufferedReader(isr);// 从字节中读取文本
while ((line= br.readLine()) != null) {
if(line.contains("TTL")) {
res= true;
break;
}
}
is.close();
isr.close();
br.close();
if (res){
System.out.println("ping通 ...");
} else{
System.out.println("ping不通...");
}
} catch (IOException e) {
System.out.println(e);
runtime.exit(1);
}
//方法二 下面代码为 JDK1.5PING的新方法但不能用,因为 该PING请求端口为7 而大型网站会关闭不需要的端口防止入侵
InetAddress address;
try {
address =InetAddress.getByName("www.weibo.com");
System.out.println("Name:" + address.getHostName());
System.out.println("Addr:" + address.getHostAddress());
System.out.println("Reach:" + address.isReachable(3000)); //是否能通信 返回true或false
System.out.println(address.toString());
} catch (Exception e) {
e.printStackTrace();
}
相关文章推荐
- java学习笔记第二章
- java中将BigInteger转换成int
- model1和model2
- IT增值服务,客户案例(一)--山东青岛在职人士,2年.Net经验,转Java开发半年
- leetCode power of two
- MyEclipse 2015 Stable 2.0 在线安装SVN插件(Subclipse)
- spring注解注入的学习
- MyEclipse性能调优初体验
- RandLoop ececute failed with exception (class java.lang.UnsatisfiedLinkError):dalvik.system.PathClas
- 为benchmarksql的PostgreSQL java驱动进行升级
- JAVA_NEW和赋值的区别
- 浅谈Java中的对象和对象引用
- 一、基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收
- 设计模式:观察者模式(基于 Java)
- spring boot--creating your own auto-configuration
- JAVA学习日记(1)
- 自己记一点springMVC的注解
- 数组转换集合
- Quartz+spring实例应用(受教育其他博主的文章,在原来基础之上进行改正)
- SpringMVC拦截器---解决乱码例子(一)