返回 登录
0

Java测试网络连通性

阅读2872

第一种方式:利用java运行时:
Java代码

 /**
 * 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;
    }
    /**
     * 测试localInetAddr能否与远程的主机指定端口建立连接相连
     * 
     * @param localInetAddr
     * @param remoteInetAddr
     * @param port
     * @param timeout
     * @return
     */
    public  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);
            log.info("SUCCESS - connection established! Local: "
                    + localInetAddr.getHostAddress() + " remote: "
                    + remoteInetAddr.getHostAddress() + " port" + port);
            isReachable = true;
        } catch (IOException e) {
            log.error("FAILRE - CAN not connect! Local: "
                    + localInetAddr.getHostAddress() + " remote: "
                    + remoteInetAddr.getHostAddress() + " port" + port);
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    log.error("Error occurred while closing socket:"
                            + e.getMessage());
                }
            }
        }
        return isReachable;
    }

    /**
     * 测试localIp能否与远程的主机指定端口建立连接相连
     * 
     * @param localIp
     * @param remoteIp
     * @param port
     * @param timeout
     * @return
     */
    public  boolean isReachable(String localIp, String remoteIp,
            int port, int timeout) {
        boolean isReachable = false;
        Socket socket = null;
        InetAddress localInetAddr = null;
        InetAddress remoteInetAddr = null;
        try {
            localInetAddr = InetAddress.getByName(localIp);
            remoteInetAddr = InetAddress.getByName(remoteIp);
            socket = new Socket();
            // 端口号设置为 0 表示在本地挑选一个可用端口进行连接
            SocketAddress localSocketAddr = new InetSocketAddress(
                    localInetAddr, 0);
            socket.bind(localSocketAddr);
            InetSocketAddress endpointSocketAddr = new InetSocketAddress(
                    remoteInetAddr, port);
            socket.connect(endpointSocketAddr, timeout);
            log.info("SUCCESS - connection established! Local: "
                    + localInetAddr.getHostAddress() + " remote: "
                    + remoteInetAddr.getHostAddress() + " port" + port);
            isReachable = true;
        } catch (IOException e) {
            log.error("FAILRE - CAN not connect! Local: "
                    + localInetAddr.getHostAddress() + " remote: "
                    + remoteInetAddr.getHostAddress() + " port" + port);
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    log.error("Error occurred while closing socket:"
                            + e.getMessage());
                }
            }
        }
        return isReachable;
    }

    public static void main(String[] args) {
          if(NetworkHelper.getInstance().isReachIp("192.168.126.128")){
              log.info("=======本机可以ping通ip:"+"192.168.126.128");
          }
          else{
              log.info("=======本机ping不通ip:"+"192.168.126.128");
          }
          if(NetworkHelper.getInstance().isReachNetworkInterfaces("192.168.126.128")){
              log.info("=======本机所有网卡可以ping通ip:"+"192.168.126.128");
          }
          else{
              log.info("=======本机所有网卡ping不通ip:"+"192.168.126.128");
          }
          String localIp = NetworkHelper.getInstance().getReachableIP("192.168.126.128",8081);
          if(!StringUtils.isBlank(localIp)){
              log.info("=======本机可以与ip:"+"192.168.126.128"+",port:"+8081+"建立连接的IP:"+localIp);
          }
          else{
              log.info("=======本机不能与ip:"+"192.168.126.128"+",port:"+8081+"建立连接的IP");
          }
    }

}

控制台输出:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/lib/logback-classic-0.9.27.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/lib/slf4j-log4j12-1.7.9.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.selector.DefaultContextSelector]
2016-10-24 14:59:45,060 [main] INFO org.NetworkHelper - 192.168.126.128 is ipv4 address
2016-10-24 14:59:46,090 [main] INFO org.NetworkHelper - SUCCESS - ping 192.168.126.128 with no interface specified
2016-10-24 14:59:46,097 [main] INFO org.NetworkHelper - ——-Trying different interfaces——–
2016-10-24 14:59:46,105 [main] INFO org.NetworkHelper - =======本机可以ping通ip:192.168.126.128
2016-10-24 14:59:46,113 [main] INFO org.NetworkHelper - 192.168.126.128 is ipv4 address
2016-10-24 14:59:47,125 [main] INFO org.NetworkHelper - SUCCESS - ping 192.168.126.128 with no interface specified
2016-10-24 14:59:47,133 [main] INFO org.NetworkHelper - ——-Trying different interfaces——–
2016-10-24 14:59:47,151 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Software Loopback Interface 1, Name:lo
2016-10-24 14:59:47,159 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,166 [main] INFO org.NetworkHelper - IP: 0:0:0:0:0:0:0:1
2016-10-24 14:59:47,171 [main] INFO org.NetworkHelper - IP: 127.0.0.1
2016-10-24 14:59:47,181 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,188 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (IPv6), Name:eth0
2016-10-24 14:59:47,194 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,200 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,206 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Bluetooth Device (RFCOMM Protocol TDI), Name:net0
2016-10-24 14:59:47,213 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,218 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,224 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (PPTP), Name:net1
2016-10-24 14:59:47,229 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,236 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,243 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (IP), Name:eth1
2016-10-24 14:59:47,250 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,256 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,261 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter, Name:net2
2016-10-24 14:59:47,268 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,276 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:e18c:98b9:82bc:107a%6
2016-10-24 14:59:47,281 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,286 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Bluetooth É豸(¸öÈËÇøÓòÍø, Name:eth2
2016-10-24 14:59:47,294 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,301 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:1171:1a89:ba:393d%7
2016-10-24 14:59:47,308 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,316 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (IKEv2), Name:net3
2016-10-24 14:59:47,323 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,330 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,337 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (PPPOE), Name:ppp0
2016-10-24 14:59:47,345 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,352 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,359 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft Virtual WiFi Miniport Adapter, Name:net4
2016-10-24 14:59:47,364 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,370 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,376 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft ISATAP Adapter #3, Name:net5
2016-10-24 14:59:47,382 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,388 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,395 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft ISATAP Adapter #2, Name:net6
2016-10-24 14:59:47,402 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,408 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,415 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft Hosted Network Virtual Adapter, Name:net7
2016-10-24 14:59:47,422 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,428 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,434 [main] INFO org.NetworkHelper - Checking interface, DisplayName:TeamViewer VPN Adapter, Name:eth3
2016-10-24 14:59:47,439 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,444 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:1192:d89e:5da4:9235
2016-10-24 14:59:47,451 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,456 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (Network Monitor), Name:eth4
2016-10-24 14:59:47,461 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,466 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,472 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft ISATAP Adapter #4, Name:net8
2016-10-24 14:59:47,478 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,483 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:0:5efe:a10:76b
2016-10-24 14:59:47,487 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,498 [main] INFO org.NetworkHelper - Checking interface, DisplayName:VMware Virtual Ethernet Adapter for VMnet1, Name:eth5
2016-10-24 14:59:47,504 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,511 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:a185:c818:9f60:a233
2016-10-24 14:59:47,518 [main] INFO org.NetworkHelper - IP: 192.168.10.1
2016-10-24 14:59:47,525 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,532 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (SSTP), Name:net9
2016-10-24 14:59:47,538 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,543 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,551 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft Teredo Tunneling Adapter, Name:net10
2016-10-24 14:59:47,557 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,562 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:0:100:7f:fffe
2016-10-24 14:59:47,567 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,573 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft ISATAP Adapter, Name:net11
2016-10-24 14:59:47,579 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:47,586 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:0:5efe:c0a8:7e01%20
2016-10-24 14:59:47,593 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:47,600 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Realtek PCIe GBE Family Controller, Name:eth6
2016-10-24 14:59:48,614 [main] INFO org.NetworkHelper - SUCCESS - ping 192.168.126.128
2016-10-24 14:59:48,620 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:7c41:2547:42e6:c961%21
2016-10-24 14:59:48,627 [main] INFO org.NetworkHelper - IP: 10.16.35.5
2016-10-24 14:59:48,632 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,639 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Microsoft Kernel Debug Network Adapter, Name:eth7
2016-10-24 14:59:48,644 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,650 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,656 [main] INFO org.NetworkHelper - Checking interface, DisplayName:WAN Miniport (L2TP), Name:net12
2016-10-24 14:59:48,664 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,670 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,675 [main] INFO org.NetworkHelper - Checking interface, DisplayName:VMware Virtual Ethernet Adapter for VMnet8, Name:eth8
2016-10-24 14:59:48,681 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,687 [main] INFO org.NetworkHelper - IP: fe80:0:0:0:5991:ed80:bda5:481b%24
2016-10-24 14:59:48,692 [main] INFO org.NetworkHelper - IP: 192.168.126.1
2016-10-24 14:59:48,697 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,702 [main] INFO org.NetworkHelper - Checking interface, DisplayName:TeamViewer VPN Adapter-WFP Native MAC Layer LightWeight Filter-0000, Name:eth9
2016-10-24 14:59:48,709 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,715 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,722 [main] INFO org.NetworkHelper - Checking interface, DisplayName:TeamViewer VPN Adapter-QoS Packet Scheduler-0000, Name:eth10
2016-10-24 14:59:48,727 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,733 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,739 [main] INFO org.NetworkHelper - Checking interface, DisplayName:TeamViewer VPN Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000, Name:eth11
2016-10-24 14:59:48,745 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,753 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,758 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Realtek PCIe GBE Family Controller-WFP Native MAC Layer LightWeight Filter-0000, Name:eth12
2016-10-24 14:59:48,764 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,770 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,776 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Realtek PCIe GBE Family Controller-QoS Packet Scheduler-0000, Name:eth13
2016-10-24 14:59:48,782 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,787 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,792 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Realtek PCIe GBE Family Controller-WFP 802.3 MAC Layer LightWeight Filter-0000, Name:eth14
2016-10-24 14:59:48,797 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,802 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,807 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter-WFP Native MAC Layer LightWeight Filter-0000, Name:net13
2016-10-24 14:59:48,815 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,822 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,828 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter-Virtual WiFi Filter Driver-0000, Name:net14
2016-10-24 14:59:48,834 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,841 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,847 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter-Native WiFi Filter Driver-0000, Name:net15
2016-10-24 14:59:48,856 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,862 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,869 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter-QoS Packet Scheduler-0000, Name:net16
2016-10-24 14:59:48,874 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,881 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,886 [main] INFO org.NetworkHelper - Checking interface, DisplayName:Broadcom BCM943228Z 802.11abgn 2x2 M.2 Wi-Fi Adapter-WFP 802.3 MAC Layer LightWeight Filter-0000, Name:net17
2016-10-24 14:59:48,890 [main] INFO org.NetworkHelper - FAILURE - ping 192.168.126.128
2016-10-24 14:59:48,895 [main] INFO org.NetworkHelper - —————–check now NetworkInterface is done————————–
2016-10-24 14:59:48,901 [main] INFO org.NetworkHelper - =======本机所有网卡ping不通ip:192.168.126.128
2016-10-24 14:59:48,912 [main] INFO org.NetworkHelper - SUCCESS - connection established! Local: 0:0:0:0:0:0:0:1 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,918 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:e18c:98b9:82bc:107a%6 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,924 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:1171:1a89:ba:393d%7 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,931 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:1192:d89e:5da4:9235 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,937 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:0:5efe:a10:76b remote: 192.168.126.128 port8081
2016-10-24 14:59:48,944 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:a185:c818:9f60:a233 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,950 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: 192.168.10.1 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,955 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:0:100:7f:fffe remote: 192.168.126.128 port8081
2016-10-24 14:59:48,960 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:0:5efe:c0a8:7e01%20 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,966 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:7c41:2547:42e6:c961%21 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,973 [main] INFO org.NetworkHelper - SUCCESS - connection established! Local: 10.16.35.5 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,980 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: fe80:0:0:0:5991:ed80:bda5:481b%24 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,986 [main] ERROR org.NetworkHelper - FAILRE - CAN not connect! Local: 192.168.126.1 remote: 192.168.126.128 port8081
2016-10-24 14:59:48,992 [main] INFO org.NetworkHelper - Reachable local IP is found, it is 10.16.35.5
2016-10-24 14:59:48,997 [main] INFO org.NetworkHelper - =======本机可以与ip:192.168.126.128,port:8081建立连接的IP:10.16.35.5
总结:
SOCKET的更靠谱,通过Socket的可以测试指定端口

学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:454297367 我们一起学Java!

评论