【工具类】测试目标设备网络是否可达

这是一个Java工具类,用于检测指定IP地址的设备或其特定端口是否可达。通过创建Socket连接尝试到目标设备的端口进行连接,从而判断网络可达性。同时,该类还提供了简单的IP可达性检查方法,通过ping操作来验证网络连通性。
摘要由CSDN通过智能技术生成

作用

有时候我们需要监控两台设备设备之间的网络是否可达,或者到目的设备的指定端口是否可达。这个工具类可以用于简单的测试。

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));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值