`
Donald_Draper
  • 浏览: 984582 次
社区版块
存档分类
最新评论

Java测试网络连通性

阅读更多
使用Java测试网络连通性的几种方法 :http://www.admin10000.com/document/1208.html
第一种方式:利用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:
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%14
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%16
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%17
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%19
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%14 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%16 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%17 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%19 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的可以测试指定端口
1
2
分享到:
评论

相关推荐

    java ftp tcp udp smtpmail 校验 连通 测试

    Java FTP、TCP、UDP、SMTP邮件验证与连接测试是网络编程中的重要环节,涉及...在实际应用中,理解这些协议的特性,选择合适的协议进行通信,以及编写有效的连通性测试代码,对于保证网络服务的稳定性和效率至关重要。

    java 测试ip是否畅通的工具类

    通过java 验证机器与某ip地址能否联通的工具类。配置ip地址就可以测试使用了。

    基于Java语言对动态连通性算法的研究.pdf

    动态连通性的应用广泛,为寻找出一种能够有效解决动态连通性问题的方法,本文基于Java语言对三种动态连通性算法进行实现和测试,通过对结果的分析,判断每种算法的运行时间及效率,选择出最为有效的解决动态连通性...

    Java代码测试IP是否为真实IP

    对于测试IP是否可达的功能,可以模拟不同网络条件,如连通的IP、不可达的IP、无效的IP,验证`isReachable()`方法的行为。 通过以上步骤,我们可以构建一个完整的Java工程,实现对IP地址的真实性和可达性的测试。在...

    采用netty编写的tcp和udp客户端和服务器端(含源码),可以生成一个网络连通性测试工具包

    net-tools.jar是一个网络连通性测试工具包,启动需要jre 1.8及以上版本支持 启动方法: java -jar net-tools.jar 在提示中选择需要启动的模块: Please select start module(1.tcpserver,2.tcpclient, 3.udpserver,...

    测试安卓应用的网络稳定性

    在安卓平台上,我们可以使用ping命令来进行网络连通性的监测。ping是一个简单但实用的网络诊断工具,通过发送ICMP(Internet Control Message Protocol)回显请求来检测网络连接的延迟和丢包情况。在应用中,我们...

    snmpv3 java实现

    java实现的简单网络管理协议!附件是整个工程,下载下来配置ip就可以运行,附有mib库,支持snmpV1,V2,V3版本协议!绝对是好资源,为了兼容移动的网络设备控制,研究了一段时间,现分享出来。不懂得可以找我

    JAVA采用S7通信协议访问西门子P JAVA西门子PLC通信,java S7-S200、java S7-300、java S7

    支持单数据读写,多数据读写,大数据量自动分包读写 支持序列化批量多地址且地址不连续的读写 支持读取DB区,I区,Q区,M区,V区 支持读取西门子S1200,200Smart 支持PLC自动重连 ...

    java 网络编程 代码集合

    Ping是一个网络诊断工具,用于检查网络连通性。11-Ping-Channel使用Java NIO(非阻塞I/O)的DatagramChannel实现,而10-Ping-Datagram则使用了传统的Socket DatagramPacket。 6. **Eclipse IDE**:00-EclipseIDE...

    java测试连接是否通畅

    在IT领域,尤其是在软件开发与网络编程中,检查URL链接的连通性是常见的需求之一。这不仅有助于确保应用程序能够顺利地与外部服务交互,也是进行自动化测试、监控系统健康状态的重要手段。以下是对给定文件中“java...

    E043-服务漏洞利用及加固-利用Java序列化漏洞进行渗透测试.pdf

    首先启动虚拟机,然后通过`ifconfig`(在Linux中)或`ipconfig`(在Windows中)获取各自的IP地址,用`ping`测试网络连通性,确保渗透机(172.16.1.4)与靶机(172.16.1.12)之间可以通信。 接下来,对靶机上的JBoss...

    java PING的综合测试

    在IT行业中,网络通信是至关重要的一个环节,而`PING`命令作为一种基本的网络诊断工具,被广泛用于检查网络连通性、测量网络延迟以及评估网络的稳定性。本篇文章将详细探讨`Java`中实现`PING`的综合测试,包括最大...

    Java 测试URL地址是否能正常连接的代码

    本文将深入解析两段Java代码,它们分别用于检查URL的连通性和可用性。 首先,我们来看第一段代码,这个方法`testWsdlConnection(String address)`用于测试一个给定的URL地址是否能正常连接: ```java public ...

    代理IP检测工具,HTTP/HTTPS/SOCKS5协议连通率和延迟检测

    1. **批量检测**:允许用户一次性输入大量代理IP,工具会逐一测试它们的连通性和延迟。 2. **协议支持**:不仅支持HTTP和HTTPS,还包括对SOCKS5协议的支持,满足不同场景需求。 3. **实时监测**:持续监控代理IP的...

    Java使用Socket判断某服务能否连通代码实例

    在这个示例中,通过创建Socket实例并尝试连接到目标服务器,我们可以有效地检测服务的连通性。这在系统监控、故障排查和自动化流程中都非常有用。同时,理解异常处理和资源管理是编写健壮网络代码的关键。

Global site tag (gtag.js) - Google Analytics