Java method for determining the type of native IP address

  • 2020-04-01 01:37:18
  • OfStack


   package net;

 import java.net.*;

 
 public class MyIp
 {
     public static void main(String[] args) throws Exception
     {
         InetAddress ia = InetAddress.getByName("www.cnblogs.com");
         byte ip[] = ia.getAddress();
         
         int[] array = new int[5];
         for(int i=0; i<ip.length; i++) {
             array[i] = (ip[i] < 0) ? 256 + ip[i]  : ip[i];

         }
         String str = TellIpType(array[0]);
         System.out.println(str);

         
     }
     
     public static String TellIpType(int num) {
         if(num<127)
             return "A";
         else if(num<192)
             return "B";
         else if(num<224)
             return "C";
         else if(num<240)
             return "D";
         else
             return "E";
     }
 }
   

Related articles: