作用
有时候我们需要监控两台设备设备之间的网络是否可达,或者到目的设备的指定端口是否可达。这个工具类可以用于简单的测试。
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
/**
* 工具类,检测到目标设备或者是目标设备的端口是否可达
*/
public class NetTestUtils {
/**
* 测试到指定IP设备的端口是否可达
* @param ip 目标设备IP
* @param port 目标设备端口
*/
public static boolean checkIPAndPortReachable(String ip, int port, int timeout) {
InetAddress inetAddress = null;
try {
inetAddress = InetAddress.getByName(ip);
} catch (UnknownHostException e) {
return false;
}
return checkIPAndPortReachable(inetAddress, port, timeout);
}
/**
* 测试到指定IP设备的端口是否可达
* @param remoteAddr 目标设备
* @param port 设备端口
*/
public static boolean checkIPAndPortReachable(InetAddress remoteAddr, int port, int timeout) {
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, timeout)) {
retIP = localAddr.getHostAddress();
break;
}
}
}
} catch (SocketException e) {
System.out.println("Error occurred while listing all the local network addresses.");
}
return retIP != null;
}
private static boolean isReachable(InetAddress localInetAddr, InetAddress remoteInetAddr, int port, int timeout) {
boolean isReachable = false;
Socket socket = null;
try {
socket = new Socket(); // 端口号设置为 0 表示在本地挑选一个可用端口进行连接
SocketAddress localSocketAddr = new InetSocketAddress(localInetAddr, 0);
socket.bind(localSocketAddr);
InetSocketAddress endpointSocketAddr = new InetSocketAddress(remoteInetAddr, port);
socket.connect(endpointSocketAddr, timeout);
isReachable = true;
} catch (IOException e) {
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
System.out.println("Error occurred while closing socket..");
}
}
}
return isReachable;
}
/**
* 简单检测IP地址是否可达
* @param ip 目标IP
*/
public static boolean checkIPReachable(String ip, int timeout) {
try {
InetAddress address = InetAddress.getByName(ip);//ping this IP
if (!(address instanceof java.net.Inet4Address) && !(address instanceof java.net.Inet6Address)) {
return false;
}
if (address.isReachable(timeout)) {
return true;
}
Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
if (address.isReachable(ni, 0, 5000)) {
return true;
}
}
} catch (Exception e) {
}
return false;
}
public static void main(String[] args) throws Exception {
//测试是否能访问目标设备的指定端口,也就是telnet 192.168.8.11 22
InetAddress inetAddress = InetAddress.getByName("192.168.8.11");
System.out.println(checkIPAndPortReachable(inetAddress, 22, 3000));
System.out.println(checkIPAndPortReachable(inetAddress, 23, 3000));
System.out.println(checkIPAndPortReachable(inetAddress, 21, 3000));
//测试本地到192.168.8.11这台设备网络是否通,也就是ping 192.168.8.11
System.out.println(checkIPReachable("192.168.8.12", 3000));
}
}