查看完整版本: [-- Java 使用Ping命令 检测网络是否为连通状态 --]

风信Java论坛 ›› Java 基础开发 ›› Java 使用Ping命令 检测网络是否为连通状态 登录 -> 注册

1F Java 使用Ping命令 检测网络是否为连通状态   jastby Post by : 2009-07-03 11:23:09.0

要用java检测网络资源是否可用,我们可以采用以下两种方法:
一种方法是调用ping命令,通用对返回数据进行分析,来探测网络资源的可用性;
这种方法有一个缺点:就是许多网络资源是不允许被ping的,从而针对这类资源无法探测。

   java代码
  1. package com.test;  
  2.  
  3. import java.io.IOException;  
  4. import java.io.InputStreamReader;  
  5. import java.io.LineNumberReader;  
  6.  
  7. public class PingTest {  
  8.  
  9.     /**  
  10.      * @param args  
  11.      * @throws IOException  
  12.      */ 
  13.     public static void main(String[] args) throws IOException {  
  14.         String address="www.javawind.net";  
  15.           
  16.         Process process = Runtime.getRuntime().exec("ping "+address);  
  17.         InputStreamReader r = new InputStreamReader(process.getInputStream());  
  18.         LineNumberReader returnData = new LineNumberReader(r);  
  19.  
  20.         String returnMsg="";  
  21.         String line = "";  
  22.         while ((line = returnData.readLine()) != null) {  
  23.             System.out.println(line);  
  24.             returnMsg += line;  
  25.         }  
  26.           
  27.         if(returnMsg.indexOf("100% loss")!=-1){  
  28.             System.out.println("与 " +address +" 连接不畅通.");  
  29.         }  
  30.         else{  
  31.             System.out.println("与 " +address +" 连接畅通.");  
  32.         }  
  33.  
  34.     }  
  35. }  

执行结果
Pinging www.javawind.net [219.129.239.184] with 32 bytes of data:

Reply from 219.129.239.184: bytes=32 time=48ms TTL=115
Reply from 219.129.239.184: bytes=32 time=50ms TTL=115
Reply from 219.129.239.184: bytes=32 time=47ms TTL=115
Reply from 219.129.239.184: bytes=32 time=48ms TTL=115

Ping statistics for 219.129.239.184:

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

Approximate round trip times in milli-seconds:

    Minimum = 47ms, Maximum = 50ms, Average = 48ms

www.javawind.net 连接畅通.

2F Re:Java 使用Ping命令 检测网络是否为连通状态   jastby Post by : 2009-07-03 11:25:27.0

方法二,转帖

Dos中的ping 命令能很方便的检测网络是否为连通状态.但是在java中,一直没有找到好的方法检来测网络是否为连通状态.无奈之余,想出了如下方法:

   java代码
  1. package com.roadway.edserver.util;  
  2.  
  3. import java.awt.Toolkit;  
  4. import java.io.InputStream;  
  5. import java.net.HttpURLConnection;  
  6. import java.net.URL;  
  7. /** *//**  
  8.  * @Description:本类开启一个线程检测网络是否连通  
  9.  * @Author : 惠万鹏  
  10.  * @Time :2008-1-10  
  11.  */ 
  12. public class NetworkManagement implements Runnable {  
  13.     private int htmlCodeSize;  
  14.     private int sleepMillisecond;  
  15.     private int sleepMillisecondWhenNetWorkUnLinked;  
  16.     private boolean isSpontaneousNotice;  
  17.     private static boolean networkIsLinked;  
  18.     private Thread thread = new Thread(this);  
  19.     private Toolkit toolkit;  
  20.     private String[] urls;  
  21.  
  22.     public NetworkManagement() {  
  23.         this.urls = new String[]{"http://www.baidu.com""http://www.google.cn"};  
  24.         this.htmlCodeSize = 50;  
  25.         this.sleepMillisecond = 5000;  
  26.         this.sleepMillisecondWhenNetWorkUnLinked = 10000;  
  27.         this.toolkit = Toolkit.getDefaultToolkit();  
  28.         thread.start();  
  29.     }  
  30.     public void setURLs(String[] urls) {  
  31.         if (urls != null && urls.length > 0) {  
  32.             this.urls = urls;  
  33.         }  
  34.     }  
  35.     public void setHtmlCodeSize(int htmlCodeSize) {  
  36.         if (htmlCodeSize > 0) {  
  37.             this.htmlCodeSize = htmlCodeSize;  
  38.         }  
  39.     }  
  40.     public void isSpontaneousNotice(boolean isSpontaneousNotice) {  
  41.         this.isSpontaneousNotice = isSpontaneousNotice;  
  42.     }  
  43.     public void setSleepMillisecont(int sleepMillisecont) {  
  44.         if (sleepMillisecont > 100) {  
  45.             this.sleepMillisecond = sleepMillisecont;  
  46.         }  
  47.     }  
  48.     public void setSleepMillisecondWhenNetWorkUnLinked(int sleepMillisecont) {  
  49.         if (sleepMillisecont > 100) {  
  50.             this.sleepMillisecondWhenNetWorkUnLinked = sleepMillisecont;  
  51.         }  
  52.     }  
  53.     public static boolean IsNetWordLinking() {  
  54.         return NetworkManagement.networkIsLinked;  
  55.     }  
  56.  
  57.     public void run() {  
  58.         while (true) {  
  59.             try {  
  60.                 this.isNetWorkLinked();  
  61.                 if (!NetworkManagement.networkIsLinked) {  
  62.                     this.isPrintMessage(this.isSpontaneousNotice);  
  63.                     Thread.sleep(this.sleepMillisecondWhenNetWorkUnLinked);  
  64.                 }  
  65.                 System.out.println(NetworkManagement.IsNetWordLinking());  
  66.                 Thread.sleep(this.sleepMillisecond);  
  67.             } catch (Exception e) {  
  68.             }  
  69.         }  
  70.     }  
  71.  
  72.     private boolean canGetHtmlCode(String httpUrl) {  
  73.         String htmlCode = "";  
  74.         try {  
  75.             InputStream in;  
  76.             URL url = new java.net.URL(httpUrl);  
  77.             HttpURLConnection connection = (HttpURLConnection) url  
  78.                     .openConnection();  
  79.             connection = (HttpURLConnection) url.openConnection();  
  80.             connection.setRequestProperty("User-Agent""Mozilla/4.0");  
  81.             connection.connect();  
  82.             in = connection.getInputStream();  
  83.             byte[] buffer = new byte[this.htmlCodeSize];  
  84.             in.read(buffer);  
  85.             htmlCode = new String(buffer);  
  86.         } catch (Exception e) {  
  87.         }  
  88.         if (htmlCode == null || htmlCode.equals("")) {  
  89.             return false;  
  90.         }  
  91.         return true;  
  92.     }  
  93.  
  94.     private void isNetWorkLinked() {  
  95.         boolean tempIsNetWorkLinked = false;  
  96.         for (int urlsCount = 0; urlsCount < this.urls.length; urlsCount++) {  
  97.             if (this.canGetHtmlCode(this.urls[urlsCount])) {  
  98.                 tempIsNetWorkLinked = true;  
  99.                 break;  
  100.             }  
  101.         }  
  102.         NetworkManagement.networkIsLinked = tempIsNetWorkLinked;  
  103.     }  
  104.     private void isPrintMessage(boolean isPrint) {  
  105.         if (isPrint) {  
  106.             toolkit.beep();  
  107.             StringBuffer message = new StringBuffer();  
  108.             message.append("------------->");  
  109.             message.append("网络中断, ");  
  110.             message.append(this.sleepMillisecondWhenNetWorkUnLinked);  
  111.             message.append(" 毫秒后再次检测!<-------------");  
  112.             System.out.println(message.toString());  
  113.         }  
  114.  
  115.     }  
  116.     public static void main(String[] args) {  
  117.         NetworkManagement n = new NetworkManagement();  
  118.         n.isSpontaneousNotice(true);  
  119.     }  

风信Java论坛 ›› Java 基础开发 ›› Java 使用Ping命令 检测网络是否为连通状态 登录 -> 注册

查看完整版本: [-- Java 使用Ping命令 检测网络是否为连通状态 --]
CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 9 ms,0 (Queries)  Gzip enabled
粤ICP备07511478号