import java.net.*;
import java.io.*;
public class ip_host {
public static void main(String args[]) throws Exception {
System.out.println("Enter the host name :");
String n = new DataInputStream(System.in).readLine();
InetAddress ipadd = InetAddress.getByName(n);
System.out.println("IP address :" + ipadd);
}
}
我有一个可以查找 IP 地址的程序,但我想扩展它来查找 IP 类别。
最佳答案
您可以通过提取地址的第一个三元组并检查适当的范围来手动执行此操作。
InetAddress address = InetAddress.getByName(host);
String firstTriplet = address.getHostAddress().
substring(0,address.getHostAddress().indexOf('.'));
if (Integer.parseInt(firstTriplet) < 128) {
System.out.println("Class A IP");
} else if (Integer.parseInt(firstTriplet) < 192) {
System.out.println("Class B IP");
} else {
System.out.println("Class C IP");
}
编辑: 固定类别
关于java - 如何检查IP地址范围是否属于A类、B类、C类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21957485/