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. ROS编译:catkin简析

    博客转载自:https://blog.csdn.net/zyh821351004/article/details/50388429 Catkin tutorials: http://wiki.ros. ...

  2. My First JS Page

    哗啦啦~我的处女作终于浮出水面了^ ^值得高兴一下,虽然参考了人家的代码.给我的感觉JS就是用来实现动态网页的,比如说弹出一个框框,然后调用JS,返回些东西. 1.打开新写好的页面a.html,弹出了 ...

  3. 验证码测试-demo

    <!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...

  4. [GO]变量内存和变量地址

    package main import "fmt" func main() { //每个变量都有两层含义,变量的内存和变量的地址 fmt.Printf("a = %d\n ...

  5. NSString 对象保存在哪? @“xxx”和 stringWithFormat:@"xxx" 区别?

    NSString *str1=@"string";//这种是保存在常量池 NSString *str2=@"string"; NSLog(@"str1 ...

  6. HTML5移动应用开发入门经典 中文pdf扫描版

    HTML5是关注度ZUI高的前沿Web技术,而移动互联网则是近两年ZUI炙手可热的Web领域.<HTML5移动应用开发入门经典>将这两者巧妙结合起来,详细讲解了如何利用HTML5进行移动应 ...

  7. 用css画的一个图形 空心正方形+四边四色

    div{ width: 100px; height: 100px; border: 100px solid black; border-left-color:darkcyan; border-righ ...

  8. winform treeview绑定数据 DOM操作

    form1 public void treeView() { // datatable 定义变量接收 传归来的值 DataTable Father = new BuMenDA().ConSql(); ...

  9. C#Thread学习

    一.Thread的使用方式 1.不带参数 (1)使用lambda public static void fun1() { Console.WriteLine($"Main ThreadId: ...

  10. Jenkins 自动化部署asp.net

    使用步骤 1.安装jenkins.git和vs,并确保机器上安装了.net framework 4.5和.net framework4.0 ,完成后访问http://localhost:8080. 2 ...