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的更多相关文章

  1. 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 ...

  2. 我的小工具开源一下-PingTest

    v博客前言 先交代下背景,最近我们项目组的网络真是太渣了,时常remote不了另外一个地方的机器,过个几分钟就断开连接,太烦躁了,严重影响工作心情...于是想着做个工具记录下每天的断开remote连接 ...

  3. Windows和Linux下 Java开发ping工具类

    package com.test.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  4. Java 实现判断 主机是否能 ping 通

    Java 实现判断 主机是否能 ping 通 代码实现如下: import java.io.IOException; import java.net.InetAddress; import java. ...

随机推荐

  1. xgboost 调参 !

    https://jessesw.com/XG-Boost/ http://blog.csdn.net/u010414589/article/details/51153310

  2. Python基础入门-While循环

    讲完了for循环我们继续来看第二个循环,那就是while循环,while循环和for循环虽然都是循环,但是有着本质的不同.我们先来看下她们之间的区别和联系: While循环和for循环区别: 1.fo ...

  3. PHP解决跨域访问的问题

    在控制器的第一行放如下代码即可解决 header('Access-Control-Allow-Origin: http://ding.taozugong.com'); header('Access-C ...

  4. System.Web.UI.Page事件执行顺序

    #region OnPreInit 第一步(显式重写,文章下面有隐式重写) protected override void OnPreInit(EventArgs e) { //检查 IsPostBa ...

  5. XE中FMX操作ListBox,添加上千条记录(含图片)

    我之前是想在ListBox的每个Item上添加一个图片,Item上所有的内容都是放在Object里赋值,结果发现加载一百条记录耗时四五秒: procedure TMainForm.AddItem; v ...

  6. .netcore项目部署IIS问题

    1.一般根据项目安装对应的runtime  下载地址:https://www.microsoft.com/net/download 2.如果出现502错误可以在cmd命令里面运行如下 如果报错,找到对 ...

  7. C# 常用正则验证[转]

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. MVPArms MVP快速集成框架

    前言 今年的Android技术圈中MVP,Dagger2,Rxjava,Retrofit这些词汇非常火,随便打开一个技术论坛都充斥着大量的关于这些技术的文章,Github也充斥着各种以基于MVP+Re ...

  9. docker--基本命令

    仅做学习参考,可能有误 part1:启动docker服务 在Windows上使用MySQL时候,有时无法直接使用MySQL -uroot -p 来进入MySQL,这是因为我们没有启动会MySQL服务此 ...

  10. 初学python - 常见函数使用

    *** 读入两个整数 a,b=eval(input()) ***range()函数 创建列表 :range( 1, 11,2) - 产生 [1,11) 相差为2数,2为步长 ***print()函数 ...