liangoogle

liangoogle
随笔 - 9, 文章 - 0, 评论 - 3, 引用 - 0
数据加载中……

查询IP地址是IPV4还是IPV6,查询IP的类别

import java.net.*;
import java.io.*;

public class IPVersion {
public static void main(String args[]) {
try {
InetAddress inetadd = InetAddress.getLocalHost();
byte[] address = inetadd.getAddress();
System.out.println(address);
if (address.length == 4) {
System.out.println("The ip version is ipv4");
int firstbyte = address[0];
if (firstbyte < 0)
firstbyte += 256;
if ((firstbyte & 0x80) == 0)
System.out.println("the ip class is A");
else if ((firstbyte & 0xC0) == 0x80)
System.out.println("The ip class is B");
else if ((firstbyte & 0xE0) == 0xC0)
System.out.println("The ip class is C");
else if ((firstbyte & 0xF0) == 0xE0)
System.out.println("The ip class is D");
else if ((firstbyte & 0xF8) == 0xF0)
System.out.println("The ip class is E");
} else if (address.length == 16)
System.out.println("The ip version is ipv6");
} catch (Exception e) {
}
;
}
}

posted on 2011-04-21 19:14 haojinlian 阅读(1082) 评论(1)  编辑  收藏

评论

# re: 查询IP地址是IPV4还是IPV6,查询IP的类别  回复  更多评论   

lkj
2013-02-05 14:47 | IPV6

只有注册用户登录后才能发表评论。


网站导航: