java测试ping通源码

PING命令参数详解

《ping的参数!》  

ping [-t] [-a] [-n count] [-l length] [-f] [-i ttl] [-v tos] [-r count] [-s count] [[-j computer-list] | [-k computer-list]  [-w timeout] destination-list   

Ping 命令可以用来验证与远程计算机的连接。(该命令只有在安装了TCP/IP协议后才能使用)   

【参数说明】 :  

-t :一直Ping指定的计算机,直到从键盘按下Ctrl+C中断。  

-a :将地址解析为计算机NetBios名。 

-n :发送count指定的ECHO数据包数,通过这个命令可以自己定义发送的个数,对衡量网络速度很有帮助。能够测试发送数据包的返回平均时间,及时间的快慢程度。默认值为 4。  

-l :发送指定数据量的ECHO数据包。默认为 32 字节;最大值是65500byte。  

-f :在数据包中发送“不要分段”标志,数据包就不会被路由上的网关分段。通常你所发送的数据包都会通过路由分段再发送给对方,加上此参数以后路由就不会再分段处理。  

-i :将“生存时间”字段设置为TTL指定的值。指定TTL值在对方的系统里停留的时间。同时检查网络运转情况的。  

-v :tos 将“服务类型”字段设置为 tos 指定的值。  

-r :在“记录路由”字段中记录传出和返回数据包的路由。通常情况下,发送的数据包是通过一系列路由才到达目标地址的,通过此参数可以设定,想探测经过路由的个数。限定能跟踪到9个路由。  

-s :指定 count 指定的跃点数的时间戳。与参数-r差不多,但此参数不记录数据包返回所经过的路由,最多只记录4个。  

-j :利用 computer-list 指定的计算机列表路由数据包。连续计算机可以被中间网关分隔(路由稀疏源) IP 允许的最大数量为 9。  

-k :computer-list 利用 computer-list 指定的计算机列表路由数据包。连续计算机不能被中间网关分隔(路由严格源)IP 允许的最大数量为 9。  

-w:timeout 指定超时间隔,单位为毫秒。  destination-list:指定要 ping 的远程计算机。  一般情况下,通过ping目标地址,可让对方返回TTL值的大小,通过TTL值可以粗略判断目标主机的系统类型是Windows还是UNIX/Linux,一般情况下Windows系统返回的TTL值在100-130之间,而UNIX/Linux系统返回的TTL值在240-255之间。但TTL的值是可以修改的。故此种方法可作为参考.   

【一般操作方法如下】:  

C:\>ping www.yahoo com   

Pinging www.yahoo.akadns net [66.218.71.81] with 32 bytes of data:   

Reply from 66.218.71.81: bytes=32 time=160ms TTL=41   

Reply from 66.218.71.81: bytes=32 time=150ms TTL=41   

Reply from 66.218.71.81: bytes=32 time=160ms TTL=41   

Reply from 66.218.71.81: bytes=32 time=161ms TTL=41   

Ping statistics for 66.218.71.81:   

Packets: Sent = 4,Received = 4,Lost = 0 (0% loss),Approximate   

round trip times in milli-seconds:   

Minimum = 150ms,Maximum = 161ms,Average = 157ms

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class PingTest {
    
    /**
    * 能否ping通IP地址
    * @param server IP地址
    * @param timeout 超时时长
    * @return true能ping通
    */
   public static boolean pingServer(String server, int timeout) {
       BufferedReader in = null;
       Runtime r = Runtime.getRuntime();

       String pingCommand = "ping " + server + " -n 1 -w " + timeout;
       try {
           Process p = r.exec(pingCommand);
           if (p == null) {
               return false;
           }
           in = new BufferedReader(new InputStreamReader(p.getInputStream()));
           String line = null;
           while ((line = in.readLine()) != null) {
               if (line.startsWith("Reply from")) {
                   return true;
               }
           }

       } catch (Exception ex) {
           ex.printStackTrace();
           return false;
       } finally {
           try {
               in.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       return false;
   }

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println(pingServer("74.125.128.190",10));

    }

}


==============================================================================================

method two

      public static boolean pingServer(String address) {
           Process process=null;
            try {
                process = Runtime.getRuntime().exec("ping "+address);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
                InputStreamReader r = new InputStreamReader(process.getInputStream());  
                LineNumberReader returnData = new LineNumberReader(r);  
        
                String returnMsg="";  
                String line = "";  
                try {
                    while ((line = returnData.readLine()) != null) {  
                       // System.out.println(line);  
                        returnMsg += line;  
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }  
                  
                if(returnMsg.indexOf("100% loss")!=-1){  
                  //  System.out.println("与 " +address +" 连接不畅通.");  
                    return false;
                }  
                else{  
                 //   System.out.println("与 " +address +" 连接畅通.");  
                    return true;
                }  
        
            } 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值