java 实现网络检测

用windows 的命令
class MyPing extends Thread{
private User user=null;
public void run() {
Process process;
boolean status=false;
try {
process = Runtime.getRuntime().exec("ping "+user.getIp());

InputStreamReader r = new InputStreamReader(process.getInputStream());
BufferedReader returnData = new BufferedReader(r);

String returnMsg="";
String line = "";

for(int i=0;i<6;i++)returnData.readLine();
returnMsg += returnData.readLine();
if(returnMsg.indexOf("Reply from "+user.getIp())!=-1){
status=true;
}
else{
status=false;
}
} catch (IOException e) {
e.printStackTrace();
}
user.setOk(status);
System.out.println((user.getName()+"状态:"+user.isOk()+"<br/>"));
return;

}
public MyPing(User user) {
this.user = user;
this.setPriority(10);
}


}
java检测
package com.roadway.edserver.util;

import java.awt.Toolkit;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/** *//**
* @Description:本类开启一个线程检测网络是否连通
* @Author : 惠万鹏
* @Time :2008-1-10
*/
public class NetworkManagement implements Runnable {
private int htmlCodeSize;
private int sleepMillisecond;
private int sleepMillisecondWhenNetWorkUnLinked;
private boolean isSpontaneousNotice;
private static boolean networkIsLinked;
private Thread thread = new Thread(this);
private Toolkit toolkit;
private String[] urls;

public NetworkManagement() {
this.urls = new String[]{"http://www.baidu.com", "http://www.google.cn"};
this.htmlCodeSize = 50;
this.sleepMillisecond = 5000;
this.sleepMillisecondWhenNetWorkUnLinked = 10000;
this.toolkit = Toolkit.getDefaultToolkit();
thread.start();
}
public void setURLs(String[] urls) {
if (urls != null && urls.length > 0) {
this.urls = urls;
}
}
public void setHtmlCodeSize(int htmlCodeSize) {
if (htmlCodeSize > 0) {
this.htmlCodeSize = htmlCodeSize;
}
}
public void isSpontaneousNotice(boolean isSpontaneousNotice) {
this.isSpontaneousNotice = isSpontaneousNotice;
}
public void setSleepMillisecont(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecond = sleepMillisecont;
}
}
public void setSleepMillisecondWhenNetWorkUnLinked(int sleepMillisecont) {
if (sleepMillisecont > 100) {
this.sleepMillisecondWhenNetWorkUnLinked = sleepMillisecont;
}
}
public static boolean IsNetWordLinking() {
return NetworkManagement.networkIsLinked;
}

public void run() {
while (true) {
try {
this.isNetWorkLinked();
if (!NetworkManagement.networkIsLinked) {
this.isPrintMessage(this.isSpontaneousNotice);
Thread.sleep(this.sleepMillisecondWhenNetWorkUnLinked);
}
System.out.println(NetworkManagement.IsNetWordLinking());
Thread.sleep(this.sleepMillisecond);
} catch (Exception e) {
}
}
}

private boolean canGetHtmlCode(String httpUrl) {
String htmlCode = "";
try {
InputStream in;
URL url = new java.net.URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0");
connection.connect();
in = connection.getInputStream();
byte[] buffer = new byte[this.htmlCodeSize];
in.read(buffer);
htmlCode = new String(buffer);
} catch (Exception e) {
}
if (htmlCode == null || htmlCode.equals("")) {
return false;
}
return true;
}

private void isNetWorkLinked() {
boolean tempIsNetWorkLinked = false;
for (int urlsCount = 0; urlsCount < this.urls.length; urlsCount++) {
if (this.canGetHtmlCode(this.urls[urlsCount])) {
tempIsNetWorkLinked = true;
break;
}
}
NetworkManagement.networkIsLinked = tempIsNetWorkLinked;
}
private void isPrintMessage(boolean isPrint) {
if (isPrint) {
toolkit.beep();
StringBuffer message = new StringBuffer();
message.append("------------->");
message.append("网络中断, ");
message.append(this.sleepMillisecondWhenNetWorkUnLinked);
message.append(" 毫秒后再次检测!<-------------");
System.out.println(message.toString());
}

}
public static void main(String[] args) {
NetworkManagement n = new NetworkManagement();
n.isSpontaneousNotice(true);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值