Advertisement
networkstatic

Verify IPv4 and resolve hostname in Java

Oct 16th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1.  
  2.  
  3.  
  4.                 String OFController = (String)configs.get(ConfigConstants.DEST_IP);
  5.                 String OFPort = (String)configs.get(ConfigConstants.CUSTOM);
  6.  
  7.                 if (_validateIP(OFController) != true) {
  8.                     logger.info("Attempting to resolve <" + OFController +
  9.                             "> to a valid IP address");
  10.                     try {
  11.                         OFController = InetAddress.getByName(OFController).getHostAddress();
  12.                     }  catch (Exception e) {
  13.                         e.printStackTrace();
  14.                         logger.error("Invalid Address <" + OFController +
  15.                                 "> Please enter valid IP-Address");
  16.                         return false;
  17.                     }
  18.                 }
  19.  
  20.  
  21.  
  22.     public boolean _validateIP(String ipAddress) {
  23.         String[] octets = ipAddress.split("\\.");
  24.         if (octets.length != 4) {
  25.             return false;
  26.         }
  27.         for (String octect : octets) {
  28.             int i = Integer.parseInt(octect);
  29.             if ((i < 0) || (i > 255)) {
  30.                 return false;
  31.             }
  32.         }
  33.         return true;
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement