java public static boolean judgeIP(JTextField ipText, JFrame jf) {
String ipAddress = ipText.getText();
String[] ip = ipAddress.split("\\.");
if (ip.length != 4) {
return false;
}
for (int i = 0; i < 4; i++)
{
int dress = 0;
try
{
dress = Integer.parseInt(ip[i]);
}
catch (NumberFormatException e1)
{
return false;
}
if ((dress >= 0) && (dress <= 255))
{
if (i == 3)
{
System.out.println("IP地址【" + ipAddress + "】合法");
return true;
}
}
else {
return false;
}
}
return false;
}
java// (子网掩码,所属ip分类)
public static boolean judgeMark(String subMark, String ipCategory)
{
String[] split = subMark.split("\\.");
String binartIp = "";
for (int i = 0; i < 4; i++)
{
int parseInt = Integer.parseInt(split[i]);
String str = MyToBinary.toBinary(parseInt);
binartIp = binartIp + str;
if (i < 3) {
binartIp = binartIp + ".";
}
}
int index0 = binartIp.indexOf("0");
int index1 = binartIp.lastIndexOf("1");
if (index0 < index1)
{
System.out.println("子网掩码【" + binartIp + "】非法");
return false;
}
if (ipCategory.equals("A"))
{
if (Integer.parseInt(split[0]) != 255) {
return false;
}
}
else if (ipCategory.equals("B"))
{
if ((Integer.parseInt(split[0]) != 255) || (Integer.parseInt(split[1]) != 255)) {
return false;
}
}
else if (ipCategory.equals("C")) {
if ((Integer.parseInt(split[0]) != 255) || (Integer.parseInt(split[1]) != 255) || (Integer.parseInt(split[2]) != 255)) {
return false;
}
}
System.out.println("子网掩码【" + binartIp + "】合法");
return true;
}
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!