码迷,mamicode.com
首页 > 编程语言 > 详细

Java测试网络连通性

时间:2016-10-25 20:19:19      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:代码生成器   单表   多表   springmvc   ssm   

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

Java代码  技术分享

  1.  /** 

  2.  * test network 

  3.  * @param ip 

  4.  */  

  5. private void getNetworkState(String ip) {  

  6.     Runtime runtime = Runtime.getRuntime();  

  7.     try {  

  8.         log.info("=================正在测试网络连通性ip:"+ip);  

  9.         Process process = runtime.exec("ping " +ip);  

  10.         InputStream iStream = process.getInputStream();  

  11.         InputStreamReader iSReader = new InputStreamReader(iStream,"UTF-8");  

  12.         BufferedReader bReader = new BufferedReader(iSReader);  

  13.         String line = null;  

  14.         StringBuffer sb = new StringBuffer();  

  15.         while ((line = bReader.readLine()) != null) {  

  16.             sb.append(line);  

  17.         }  

  18.         iStream.close();  

  19.         iSReader.close();  

  20.         bReader.close();  

  21.         String result  = new String(sb.toString().getBytes("UTF-8"));  

  22.         log.info("ping result:"+result);  

  23.         if (!StringUtils.isBlank(result)) {  

  24.             if (result.indexOf("TTL") > 0 || result.indexOf("ttl") > 0) {  

  25.                 log.info("网络正常,时间: " + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));           

  26.             } else {  

  27.                 log.info("网络断开,时间 :" + TimeUtil.getCurDate("yyyy-mm-dd hh:mm:ss"));  

  28.               

  29.             }  

  30.         }  

  31.     } catch (Exception e) {  

  32.         log.error("网络异常:"+e.getMessage());  

  33.         e.printStackTrace();  

  34.     }  

  35. }  


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

Java代码  技术分享

  1. package com.util.network;  

  2.   

  3. import java.io.IOException;  

  4. import java.net.InetAddress;  

  5. import java.net.InetSocketAddress;  

  6. import java.net.NetworkInterface;  

  7. import java.net.Socket;  

  8. import java.net.SocketAddress;  

  9. import java.net.SocketException;  

  10. import java.net.UnknownHostException;  

  11. import java.util.Enumeration;  

  12.   

  13. import org.apache.commons.lang.StringUtils;  

  14. import org.slf4j.Logger;  

  15. import org.slf4j.LoggerFactory;  

  16.   

  17. /** 

  18.  * 测试网络连通性 

  19.  *  

  20.  * @author donald 

  21.  *  

  22.  */  

  23. public class NetworkHelper {  

  24.     private static Logger log = LoggerFactory.getLogger(NetworkHelper.class);  

  25.     private static NetworkHelper instance = null;  

  26.     public static synchronized NetworkHelper getInstance(){  

  27.         if(instance == null){  

  28.             instance = new NetworkHelper();  

  29.         }  

  30.         return instance;  

  31.           

  32.     }  

  33.   下载

  34.     /** 

  35.      * 测试本地能否ping ip 

  36.      *  

  37.      * @param ip 

  38.      * @return 

  39.      */  

  40.     public boolean isReachIp(String ip) {  

  41.         boolean isReach = false;  

  42.         try {  

  43.             InetAddress address = InetAddress.getByName(ip);// ping this IP  

  44.   

  45.             if (address instanceof java.net.Inet4Address) {  

  46.                 log.info(ip + " is ipv4 address");  

  47.             } else if (address instanceof java.net.Inet6Address) {  

  48.                 log.info(ip + " is ipv6 address");  

  49.             } else {  

  50.                 log.info(ip + " is unrecongized");  

  51.             }  

  52.             if (address.isReachable(5000)) {  

  53.                 isReach = true;  

  54.                 log.info("SUCCESS - ping " + ip  

  55.                         + " with no interface specified");  

  56.             } else {  

  57.                 isReach = false;  

  58.                 log.info("FAILURE - ping " + ip  

  59.                         + " with no interface specified");  

  60.             }  

  61.         } catch (Exception e) {  

  62.             log.error("error occurs:" + e.getMessage());  

  63.         }  

  64.         return isReach;  

  65.     }  

  66.   

  67.     /** 

  68.      * 测试本地所有的网卡地址都能ping通 ip 

  69.      *  

  70.      * @param ip 

  71.      * @return 

  72.      */  

  73.     public  boolean isReachNetworkInterfaces(String ip) {  

  74.         boolean isReach = false;  

  75.         try {  

  76.             InetAddress address = InetAddress.getByName(ip);// ping this IP  

  77.   

  78.             if (address instanceof java.net.Inet4Address) {  

  79.                 log.info(ip + " is ipv4 address");  

  80.             } else if (address instanceof java.net.Inet6Address) {  

  81.                 log.info(ip + " is ipv6 address");  

  82.             } else {  

  83.                 log.info(ip + " is unrecongized");  

  84.             }  

  85.             if (address.isReachable(5000)) {  

  86.                 isReach = true;  

  87.                 log.info("SUCCESS - ping " + ip  

  88.                         + " with no interface specified");  

  89.             } else {  

  90.                 isReach = false;  

  91.                 log.info("FAILURE - ping " + ip  

  92.                         + " with no interface specified");  

  93.             }  

  94.             if (isReach) {  

  95.                 log.info("-------Trying different interfaces--------");  

  96.                 Enumeration<NetworkInterface> netInterfaces = NetworkInterface  

  97.                         .getNetworkInterfaces();  

  98.                 while (netInterfaces.hasMoreElements()) {  

  99.                     NetworkInterface ni = netInterfaces.nextElement();  

  100.                     log.info("Checking interface, DisplayName:"  

  101.                             + ni.getDisplayName() + ", Name:" + ni.getName());  

  102.                     if (address.isReachable(ni, 05000)) {  

  103.                         isReach = true;  

  104.                         log.info("SUCCESS - ping " + ip);  

  105.                     } else {  

  106.                         isReach = false;  

  107.                         log.info("FAILURE - ping " + ip);  

  108.                     }  

  109.                     Enumeration<InetAddress> ips = ni.getInetAddresses();  

  110.                     while (ips.hasMoreElements()) {  

  111.                         log.info("IP: " + ips.nextElement().getHostAddress());  

  112.                     }  

  113.                     log.info("-----------------check now NetworkInterface is done--------------------------");  

  114.                 }  

  115.             }  

  116.         } catch (Exception e) {  

  117.             log.error("error occurs:" + e.getMessage());  

  118.         }  

  119.         return isReach;  

  120.     }  

  121.   下载

  122.     /** 

  123.      * 获取能与远程主机指定端口建立连接的本机ip地址 

  124.      * @param remoteAddr 

  125.      * @param port 

  126.      * @return 

  127.      */  

  128.     public  String getReachableIP(InetAddress remoteAddr, int port) {  

  129.         String retIP = null;  

  130.         Enumeration<NetworkInterface> netInterfaces;  

  131.         try {  

  132.             netInterfaces = NetworkInterface.getNetworkInterfaces();  

  133.             while (netInterfaces.hasMoreElements()) {  

  134.                 NetworkInterface ni = netInterfaces.nextElement();  

  135.                 Enumeration<InetAddress> localAddrs = ni.getInetAddresses();  

  136.                 while (localAddrs.hasMoreElements()) {  

  137.                     InetAddress localAddr = localAddrs.nextElement();  

  138.                     if (isReachable(localAddr, remoteAddr, port, 5000)) {  

  139.                         retIP = localAddr.getHostAddress();  

  140.                         break;  

  141.                     }  

  142.                 }  

  143.             }  

  144.         } catch (SocketException e) {  

  145.             log.error("Error occurred while listing all the local network addresses:"  

  146.                     + e.getMessage());  

  147.         }  

  148.         if (retIP == null) {  

  149.             log.info("NULL reachable local IP is found!");  

  150.         } else {  

  151.             log.info("Reachable local IP is found, it is " + retIP);  

  152.         }  

  153.         return retIP;  

  154.     }  

  155.     /** 

  156.      * 获取能与远程主机指定端口建立连接的本机ip地址 

  157.      * @param remoteIp 

  158.      * @param port 

  159.      * @return 

  160.      */  

  161.     public  String getReachableIP(String remoteIp, int port) {  

  162.   

  163.         String retIP = null;  

  164.         InetAddress remoteAddr = null;  

  165.         Enumeration<NetworkInterface> netInterfaces;  

  166.         try {  

  167.             remoteAddr = InetAddress.getByName(remoteIp);  

  168.             netInterfaces = NetworkInterface.getNetworkInterfaces();  

  169.             while (netInterfaces.hasMoreElements()) {  

  170.                 NetworkInterface ni = netInterfaces.nextElement();  

  171.                 Enumeration<InetAddress> localAddrs = ni.getInetAddresses();  

  172.                 while (localAddrs.hasMoreElements()) {  

  173.                     InetAddress localAddr = localAddrs.nextElement();  

  174.                     if (isReachable(localAddr, remoteAddr, port, 5000)) {  

  175.                         retIP = localAddr.getHostAddress();  

  176.                         break;  

  177.                     }  

  178.                 }  

  179.             }  

  180.         } catch (UnknownHostException e) {  

  181.             log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());  

  182.         }catch (SocketException e) {  

  183.             log.error("Error occurred while listing all the local network addresses:"+ e.getMessage());  

  184.         }  

  185.         if (retIP == null) {  

  186.             log.info("NULL reachable local IP is found!");  

  187.         } else {  

  188.             log.info("Reachable local IP is found, it is " + retIP);  

  189.         }  

  190.         return retIP;  

  191.     }  下载

  192.     /** 

  193.      * 测试localInetAddr能否与远程的主机指定端口建立连接相连 

  194.      *  

  195.      * @param localInetAddr 

  196.      * @param remoteInetAddr 

  197.      * @param port 

  198.      * @param timeout 

  199.      * @return 

  200.      */  

  201.     public  boolean isReachable(InetAddress localInetAddr,  

  202.             InetAddress remoteInetAddr, int port, int timeout) {  

  203.         boolean isReachable = false;  

  204.         Socket socket = null;  

  205.         try {  

  206.             socket = new Socket();  

  207.             // 端口号设置为 0 表示在本地挑选一个可用端口进行连接  

  208.             SocketAddress localSocketAddr = new InetSocketAddress(  

  209.                     localInetAddr, 0);  

  210.             socket.bind(localSocketAddr);  

  211.             InetSocketAddress endpointSocketAddr = new InetSocketAddress(  

  212.                     remoteInetAddr, port);  

  213.             socket.connect(endpointSocketAddr, timeout);  

  214.             log.info("SUCCESS - connection established! Local: "  

  215.                     + localInetAddr.getHostAddress() + " remote: "  

  216.                     + remoteInetAddr.getHostAddress() + " port" + port);  

  217.             isReachable = true;  

  218.         } catch (IOException e) {  

  219.             log.error("FAILRE - CAN not connect! Local: "  

  220.                     + localInetAddr.getHostAddress() + " remote: "  

  221.                     + remoteInetAddr.getHostAddress() + " port" + port);  

  222.         } finally {  

  223.             if (socket != null) {  

  224.                 try {  

  225.                     socket.close();  

  226.                 } catch (IOException e) {  

  227.                     log.error("Error occurred while closing socket:"  

  228.                             + e.getMessage());  

  229.                 }  

  230.             }  

  231.         }  

  232.         return isReachable;  

  233.     }  

  234.   

  235.     /** 

  236.      * 测试localIp能否与远程的主机指定端口建立连接相连 

  237.      *  

  238.      * @param localIp 

  239.      * @param remoteIp 

  240.      * @param port 

  241.      * @param timeout 

  242.      * @return 

  243.      */  

  244.     public  boolean isReachable(String localIp, String remoteIp,  

  245.             int port, int timeout) {  

  246.         boolean isReachable = false;  

  247.         Socket socket = null;  

  248.         InetAddress localInetAddr = null;  

  249.         InetAddress remoteInetAddr = null;  

  250.         try {  

  251.             localInetAddr = InetAddress.getByName(localIp);  

  252.             remoteInetAddr = InetAddress.getByName(remoteIp);  

  253.             socket = new Socket();  

  254.             // 端口号设置为 0 表示在本地挑选一个可用端口进行连接  

  255.             SocketAddress localSocketAddr = new InetSocketAddress(  

  256.                     localInetAddr, 0);  

  257.             socket.bind(localSocketAddr);  

  258.             InetSocketAddress endpointSocketAddr = new InetSocketAddress(  

  259.                     remoteInetAddr, port);  

  260.             socket.connect(endpointSocketAddr, timeout);  

  261.             log.info("SUCCESS - connection established! Local: "  

  262.                     + localInetAddr.getHostAddress() + " remote: "  

  263.                     + remoteInetAddr.getHostAddress() + " port" + port);  

  264.             isReachable = true;  

  265.         } catch (IOException e) {  

  266.             log.error("FAILRE - CAN not connect! Local: "  

  267.                     + localInetAddr.getHostAddress() + " remote: "  

  268.                     + remoteInetAddr.getHostAddress() + " port" + port);  

  269.         } finally {  

  270.             if (socket != null) {  

  271.                 try {  

  272.                     socket.close();  

  273.                 } catch (IOException e) {  

  274.                     log.error("Error occurred while closing socket:"  

  275.                             + e.getMessage());  

  276.                 }  

  277.             }  

  278.         }  

  279.         return isReachable;  

  280.     }  

  281.   

  282.     public static void main(String[] args) {  

  283.           if(NetworkHelper.getInstance().isReachIp("192.168.126.128")){  

  284.               log.info("=======本机可以ping通ip:"+"192.168.126.128");  

  285.           }  

  286.           else{  

  287.               log.info("=======本机ping不通ip:"+"192.168.126.128");  

  288.           }  

  289.           if(NetworkHelper.getInstance().isReachNetworkInterfaces("192.168.126.128")){  

  290.               log.info("=======本机所有网卡可以ping通ip:"+"192.168.126.128");  

  291.           }  

  292.           else{  

  293.               log.info("=======本机所有网卡ping不通ip:"+"192.168.126.128");  

  294.           }  

  295.           String localIp = NetworkHelper.getInstance().getReachableIP("192.168.126.128",8081);  

  296.           if(!StringUtils.isBlank(localIp)){  

  297.               log.info("=======本机可以与ip:"+"192.168.126.128"+",port:"+8081+"建立连接的IP:"+localIp);  

  298.           }  

  299.           else{  

  300.               log.info("=======本机不能与ip:"+"192.168.126.128"+",port:"+8081+"建立连接的IP");  

  301.           }  

  302.     }  

  303.   

  304. }  


Java测试网络连通性

标签:代码生成器   单表   多表   springmvc   ssm   

原文地址:http://12213814.blog.51cto.com/12203814/1865301

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!