java Ping操作

来源:互联网 发布:javascript张容铭 pdf 编辑:程序博客网 时间:2024/06/06 18:30
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;




public class pingTest {


/**
* @author MrFu
*/
public static void main(String[] args) {
ping(5,"www.baidu.com");
}


public static void ping(int pingNum,String m_strForNetAddress){
String result = "";
String reStr ="";
try {
Process p = Runtime.getRuntime().exec("ping -n "+ pingNum + " -a " + m_strForNetAddress); // 10.83.50.111  m_strForNetAddress
//Process p = Runtime.getRuntime().exec("/system/bin/ping -c 3 -w 1 www.baidu.com");
int status = 5;
try {
status = p.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();


       if (status == 0) {  
           result="success"; 
       }    
       else 
       { 
           result="failed"; 
       } 
       System.out.println("result"+result);
       String lost = new String();  
       String delay = new String();  
       BufferedReader buf = new BufferedReader(new InputStreamReader(p.getInputStream(),"GB2312"));
       
       String str = new String();  
       //读出所有信息并显示                    
while((str=buf.readLine())!=null){  
   str = str + "\r\n";
   reStr = str;
   System.out.println(reStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 0
原创粉丝点击