- 浏览: 201549 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
brenda:
...
技术选型(转) -
JavaScriptOMG:
写的真好,不知道如果是java.sql.date的话,怎么写呢 ...
Java得到下一天明天,明天时间 -
少女杀手:
和他的一摸一样,一个字都不差
http://anysky131 ...
弹出窗口代码大全 -
shipping:
字体好小啊,都没兴趣看下去了
测试网站性能的30款免费在线工具 -
ddd:
其实一切人活着的意义就在于他死前的心情是什么。
活着是多么美好
要用java检测网络资源是否可用,我们可以采用以下两种方法:
一种方法是调用ping命令,
如:
Process process= Runtime.getRuntime().exec("ping 192.168.0.5");
InputStreamReader return = new InputStreamReader(process.getInputStream());
LineNumberReader returnData = new LineNumberReader (return);
String line="";
while((line=returnData.readLine())!=null){
System.out.println(line);
}
通用对返回数据进行分析,来探测网络资源的可用性;
这种方法有一个缺点:就是许多网络资源是不允许被ping的,从而针对这类资源无法探测。
(2008-07-28记)
Dos中的ping 命令能很方便的检测网络是否为连通状态.但是在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);
}
}
转自:http://hwpok.iteye.com/blog/615992
一种方法是调用ping命令,
如:
Process process= Runtime.getRuntime().exec("ping 192.168.0.5");
InputStreamReader return = new InputStreamReader(process.getInputStream());
LineNumberReader returnData = new LineNumberReader (return);
String line="";
while((line=returnData.readLine())!=null){
System.out.println(line);
}
通用对返回数据进行分析,来探测网络资源的可用性;
这种方法有一个缺点:就是许多网络资源是不允许被ping的,从而针对这类资源无法探测。
(2008-07-28记)
Dos中的ping 命令能很方便的检测网络是否为连通状态.但是在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);
}
}
转自:http://hwpok.iteye.com/blog/615992
发表评论
-
技术选型(转)
2011-05-17 15:05 11272.1. 基础架构 ... -
分布式Java 应用(转)
2011-05-17 14:43 1348网络通信:协议TCP/IP,UDP/Ip,Multicas ... -
跨域访问session(转)
2011-04-22 16:14 2639大一些的网站,通常都 ... -
(转)分享一下,我常去的中文技术网站
2011-04-18 13:31 838先说一下大多数人都知 ... -
(转) request.getPathInfo() 方法的作用
2011-04-14 11:58 886request.getPathInfo(); 这个方法返回请 ... -
找到一篇性能测试的好文,简单实用,收藏之。
2011-04-10 21:59 764Java程序性能测试 1 概述 在 ... -
需要牢记的java编程规则(收藏)
2011-04-10 20:52 728(1) 类名首字母应该 ... -
一个计算机专业学生几年的编程经验汇总之二(收藏)
2011-04-10 19:48 870############################### ... -
一个计算机专业学生几年的编程经验汇总之一(收藏)
2011-04-10 18:05 857来学习Java也有两个年头了,永远不敢说多么精通,但也想谈谈自 ... -
(转)各种架构图汇总
2011-04-06 22:27 1394转载请保留出处,刘晓涛汇总!!! http://bl ... -
(转)java并发编程实践笔记
2011-04-05 22:23 7711, 保证线程安全的三种方法 : a, 不要跨线程访问共享变量 ... -
(转)构建可伸缩,高性能的互联网应用
2011-04-05 22:22 756间过得很快,来新公司已经两个月了,在这两个月的时间里,自己也感 ... -
(转)百万级访问量网站的技术准备工作
2011-04-05 22:20 888当今从纯网站技术上来说,因为开源模式的发展,现在建一个小网站已 ... -
测试数据库连接状态
2011-03-25 08:45 1361while (true) { long star ... -
(转)什么是Java里的OO思想?
2011-03-24 14:12 886OO就是面向对象 面向对象(Object Oriented,O ... -
中文乱码问题的解决方法
2011-01-21 17:33 1158tomcat下中文的彻底解决[转] http://blo ... -
nginx 映射80端口
2009-08-04 09:13 3878配置一个resin, 为不用输入端 ... -
调整 Java 虚拟机
2009-07-09 23:43 1019尽管 JVM 调整操作随 JVM 提供程序的不同而有所变化,但 ... -
测试网站性能的30款免费在线工具
2009-06-28 02:12 2008你是否肯定你的网站完全兼容各大浏览器?是否知道多少秒可以打开你 ... -
Memcached-----memcached实现内存缓存
2009-06-27 15:38 2698Memcached是danga.com(运营LiveJourn ...
相关推荐
利用“ping”命令可以检查网络是否连通,可以很好地帮助我们分析和判定网络故障。应用格式:Ping空格IP地址。 该命令还可以加许多参数使用,具体是键入Ping按回车即可看到详细说明。 ping是一个DOS命令,一般用于...
检测网络是否连通最可靠的方法,本类使用非常方便,调用示例: Dim p As New Ping p.TimeOut = 5000 '超时,有默认值,可以省略 p.HostIP = "202.108.22.5" '要ping的IP地址,有默认值,可以省略 Dim r As Boolean = p.Is...
pb 用Ping方法 判断网络是否连通 IP地址是否有效
本文Ping是潜水艇人员的专用术语,表示回应的声纳脉冲,在网络中Ping 是一个十分好用的TCP/IP工具。它主要的功能是用来检测网络的连通情况和分析网络速度。下面我们对ping命令进行了详细解释。
这个程序是使用Ping命令来检测局域网内计算机的网络连接状态,并以图形的方式显示出结果,鼠标放在某一需要检测的计算机图标上,程序会优先检测本计算机是否连接网络,程序有不少的可取之处,希望VB学习者下载研究...
ping的C++实现类,实现网络连通检测
就是一个判断本机与某个IP地址、某个电脑是否连通,从而可以判断本机是否在某个网段上,也可以通过ping外网的地址来判断本机是否连网,也可以用来检测某个网站是否还存在可以访问。
java调用jpcap实现ping功能代码
本文档介绍了如何在Java中使用ProcessBuilder类执行ping命令,以检测主机的网络连通性。我们提供了详细的步骤说明,并附带一个简单的源码示例,演示如何使用Java代码执行ping命令并解析结果。 内容概要 本文档涵盖...
狂龙网络检测工具是一个方便实用的ping命令工具,在工作中我们常常需要测试一组网络地址是否连通,最常用的方法是利用ping命令检查网络,并帮助分析判定网络的故障。但发现大部人喜欢在运行框中输入ping命令,这样...
网络Ping 测试工具 网络联通测试 测试网络中连接是否出现短线 及实时状况
目前,Ping&Tracert作为链路连通性检测和故障定位的重要手段,适用场景极广,本次课程将选取对SRv6网络进行Ping&Tracert检测的场景,以一个示例为主线,深度解析检测过程,解密报文交互与协议扩展,通过介绍关键命令...
这是一个测试PDA与网络是否连通的工具,如WIFI连接是否正确
pb 用Ping方法 判断网络是否连通 IP地址是否有效 如有错误并显示错误编号
检测本机网络连通性的工具类: 使用TCP的7端口的Echo服务检测本机网络连通性;可设置多IP检测、检测方式、超时时间;使用简单;详细说明;
使用Ping检查网络的连通性,包括本机的TCP/IP协议、本机的IP地址设置等。
使用ping命令检测网速和网络连通状况的.docx
测试IP端口联通性,方便快捷的测试IP是否能ping通
通过java 验证机器与某ip地址能否联通的工具类。配置ip地址就可以测试使用了。
PING程序是我们使用的比较多的用于测试网络连通性的程序。PING程序基于ICMP,使用ICMP的回送请求和回送应答来工作。由计算机网络课程知道,ICMP是基于IP的一个协议,ICMP包通过IP的封装之后传递。 课程设计中选取...