pingUtil
package unit; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class pingUtil {
public static boolean ping(String ipAddress) throws Exception{
int timeOut=3000;
boolean status=InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的
return status;
} public static void ping02(String ipAddress){
String line=null; try {
Process pro=Runtime.getRuntime().exec("ping "+ipAddress);
BufferedReader buf=new BufferedReader(new InputStreamReader(pro.getInputStream(),"utf-8"));
while((line=buf.readLine())!=null){
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} public static boolean ping(String ipAddress,int pingTimes,int timeOut){
BufferedReader in=null;
Runtime r= Runtime.getRuntime();
String pingCommand = "ping "+ipAddress+" -n "+pingTimes+" -w "+timeOut; try {//执行命令并获取输出
System.out.println(pingCommand);
Process p = r.exec(pingCommand);
if(p==null){
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream(),"utf-8"));
int connectedCount=0;
String line=null;
while((line=in.readLine())!=null){
connectedCount += getCheckResult(line);
}//如果出现类似=20ms TTL=22这样的字符,出现的次数=测试次数 则返回真
return connectedCount == pingTimes;
} catch (Exception e) {
e.printStackTrace();//出现异常则返回假
return false;
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} //若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
private static int getCheckResult(String line){
Pattern pattern=Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while(matcher.find()){
return 1;
}
return 0;
} public static void main(String[] args) throws Exception{
String ipAddress="www.qq.com";
// System.out.println(ping(ipAddress));
ping02(ipAddress);
System.out.println(ping(ipAddress,5,5000));
} }
pingUtil的更多相关文章
- PingUtil in Android
Ping a host in Android:“ping -c 1 127.0.0.1”-c 1: The ping times. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- 我的小工具开源一下-PingTest
v博客前言 先交代下背景,最近我们项目组的网络真是太渣了,时常remote不了另外一个地方的机器,过个几分钟就断开连接,太烦躁了,严重影响工作心情...于是想着做个工具记录下每天的断开remote连接 ...
- Windows和Linux下 Java开发ping工具类
package com.test.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- Java 实现判断 主机是否能 ping 通
Java 实现判断 主机是否能 ping 通 代码实现如下: import java.io.IOException; import java.net.InetAddress; import java. ...
随机推荐
- 数字图像处理实验(13):PROJECT 05-04,Parametric Wiener Filter 标签: 图像处理MATLAB 2017-05-27 10:59
实验要求: Objective: To understand the high performance of the parametric Wiener Filter in image restora ...
- 235D Graph Game
传送门 题目大意 https://www.luogu.org/problemnew/show/CF235D 分析 我们先考虑它是树的情况 我们设$event(x,y)$表示删除点x是y与x联通这件事对 ...
- Luogu 4869 albus就是要第一个出场
BZOJ 2844 被NOIP模拟赛题弄自闭了QuQ. 因为本题要求异或,所以自然地构造出线性基,假设本题中给出的数有$n$个,而我们构造出的线性基大小为$m$,那么每一个可以异或出来的数相当于出现了 ...
- ServletContext作用功能详解.RP
ServletContext ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而s ...
- Oracle数据库之单表查询
接着上一篇的分享,今天主要给大家分享的是关于数据中的单表查询,单表查询很基础,也很重要,但是任何一个初学者必须要掌握的姿势,单表查询就是对单个表进行操作,查询我们想要的数据.单表查询里面的内容也是比较 ...
- 第08章-使用Spring Web Flow
使用Spring Web Flow Spring Web Flow是Spring MVC的扩展,它支持开发基于流程的应用程序.它将流程的定义与实现流程行为的类和视图分离开来. 1 在Spring中配置 ...
- 解决linux下80端口占用问题
在即安装有tomcat,又安装有nginx的服务器上(典型阿里云驻云java镜像),系统默认配置nginx占用80端口,tomcat占用8080端口. 如果想要便于用户可以直接通过IP或者域名访问到t ...
- .net Stream篇(六)
BufferedStream 目录: 简单介绍一下BufferedStream 如何理解缓冲区? BufferedStream的优势 从BufferedStream 中学习装饰模式 如何理解装饰模式 ...
- 快速获取.NET DLL文件编译时间
当用户现场汇报问题给我们, 我们比较关心的就有用户现场的DLL是什么版本号,是什么时候编译的. 有没有什么办法得到呢?办法是有的. 在网上找了很久终端找到这个软件非常地好用. 直接把文件拖到软件里就行 ...
- 以太坊系列之十二: solidity变量存储
solidity中变量的存储 变量存储主要分为两个区域,一个是storage(对应指定是SLOAD,SSTORE),一个是Memory(MLOAD,MSTORE), 这和普通编程语言的内存模型是不一样 ...