如何用java编程测试网络的鲁棒性

 我来答
EvenHHZ
2016-10-25 · 知道合伙人软件行家
EvenHHZ
知道合伙人软件行家
采纳数:13691 获赞数:18845
个人出版图书:《玩转Python网络爬虫》、《玩转Django2.0》

向TA提问 私信TA
展开全部
/**
* test network
* @param ip
*/
private void getNetworkState(String ip) {
Runtime runtime = Runtime.getRuntime();
try {
log.info("=================正在测试网络连通性ip:"+ip);
Process process = runtime.exec("ping " +ip);
InputStream iStream = process.getInputStream();
InputStreamReader iSReader = new InputStreamReader(iStream,"UTF-8");
BufferedReader bReader = new BufferedReader(iSReader);
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = bReader.readLine()) != null) {
sb.append(line);
}
iStream.close();
iSReader.close();
bReader.close();
String result = new String(sb.toString().getBytes("UTF-8"));
log.info("ping result:"+result);
if (!StringUtils.isBlank(result)) {
if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {
log.info("网络正常,时间: " + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));
} else {
log.info("网络断开,时间 :" + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));

}
}
} catch (Exception e) {
log.error("网络异常:"+e.getMessage());
e.printStackTrace();
}
}

在windows平台上,上面代码没有为,ping ip 会结束,而在linux环境中ping命令,ping不通时,
会卡住,ping通,会不定的输出信息,考虑用另一种方式socket。 下载

第二种方式socket:

Java代码
package com.util.network;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 测试网络连通性
*
* @author donald
* 下载
*/
public class NetworkHelper {
private static Logger log = LoggerFactory.getLogger(NetworkHelper.class);
private static NetworkHelper instance = null;
public static synchronized NetworkHelper getInstance(){
if(instance == null){
instance = new NetworkHelper();
}
return instance;

}

/**
* 测试本地能否ping ip
*
* @param ip
* @return
*/
public boolean isReachIp(String ip) {
boolean isReach = false;
try {
InetAddress address = InetAddress.getByName(ip);// ping this IP

if (address instanceof java.net.Inet4Address) {
log.info(ip + " is ipv4 address");
} else if (address instanceof java.net.Inet6Address) {
log.info(ip + " is ipv6 address");
} else {
log.info(ip + " is unrecongized");
}
if (address.isReachable(5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip
+ " with no interface specified");
} else {
isReach = false;
log.info("FAILURE - ping " + ip
+ " with no interface specified");
}
} catch (Exception e) {
log.error("error occurs:" + e.getMessage());
}
return isReach;
}

/**
* 测试本地所有的网卡地址都能ping通 ip
*
* @param ip
* @return
*/
public boolean isReachNetworkInterfaces(String ip) {
boolean isReach = false;
try {
InetAddress address = InetAddress.getByName(ip);// ping this IP

if (address instanceof java.net.Inet4Address) {
log.info(ip + " is ipv4 address");
} else if (address instanceof java.net.Inet6Address) {
log.info(ip + " is ipv6 address");
} else {
log.info(ip + " is unrecongized");
}
if (address.isReachable(5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip
+ " with no interface specified");
} else {
isReach = false;
log.info("FAILURE - ping " + ip
+ " with no interface specified");
}
if (isReach) {
log.info("-------Trying different interfaces--------");
Enumeration<NetworkInterface> netInterfaces = NetworkInterface
.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
log.info("Checking interface, DisplayName:"
+ ni.getDisplayName() + ", Name:" + ni.getName());
if (address.isReachable(ni, 0, 5000)) {
isReach = true;
log.info("SUCCESS - ping " + ip);
} else {
isReach = false;
log.info("FAILURE - ping " + ip);
}
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
log.info("IP: " + ips.nextElement().getHostAddress());
}
log.info("-----------------check now NetworkInterface is done--------------------------");
}
} 下载
} catch (Exception e) {
log.error("error occurs:" + e.getMessage());
}
return isReach;
}

/**
* 获取能与远程主机指定端口建立连接的本机ip地址
* @param remoteAddr
* @param port
* @return
*/
public String getReachableIP(InetAddress remoteAddr, int port) {
String retIP = null;
Enumeration<NetworkInterface> netInterfaces;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> localAddrs = ni.getInetAddresses();
while (localAddrs.hasMoreElements()) {
InetAddress localAddr = localAddrs.nextElement();
if (isReachable(localAddr, remoteAddr, port, 5000)) {
retIP = localAddr.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
log.error("Error occurred while listing all the local network addresses:"
+ e.getMessage());
}
if (retIP == null) {
log.info("NULL reachable local IP is found!");
} else {
log.info("Reachable local IP is found, it is " + retIP);
}
return retIP;
}
/**
* 获取能与远程主机指定端口建立连接的本机ip地址
* @param remoteIp
* @param port
* @return
*/
public String getReachableIP(String remoteIp, int port) {

String retIP = null;
InetAddress remoteAddr = null;
Enumeration<NetworkInterface> netInterfaces;
try {
remoteAddr = InetAddress.getByName(remoteIp);
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> localAddrs = ni.getInetAddresses();
while (localAddrs.hasMoreElements()) {
InetAddress localAddr = localAddrs.nextElement();
if (isReachable(localAddr, remoteAddr, port, 5000)) {
retIP = localAddr.getHostAddress();
break;
}
}
}
} catch (UnknownHostException e) {
log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());
}catch (SocketException e) {
log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());
}
if (retIP == null) {
log.info("NULL reachable local IP is found!");
} else {
log.info("Reachable local IP is found, it is " + retIP);
}
return retIP;
}
/**
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式