社区
Java SE
帖子详情
求IPV6函数
scvzhang
2007-07-01 09:32:59
1 IPV6检验函数 查看一个TEXT添的IPV6是否正确
2 IPV6 TO short[8]
3 IPV6 TO BYTE[16]
...全文
350
11
打赏
收藏
求IPV6函数
1 IPV6检验函数 查看一个TEXT添的IPV6是否正确 2 IPV6 TO short[8] 3 IPV6 TO BYTE[16]
复制链接
扫一扫
分享
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
火龙果被占用了
2007-07-03
打赏
举报
回复
CSDN 好慢!!!花了一个多小时又重新上来了,呵呵~~
noOnlyCode
2007-07-03
打赏
举报
回复
说错,类库,汗一个……
noOnlyCode
2007-07-03
打赏
举报
回复
有函数库没?
火龙果被占用了
2007-07-03
打赏
举报
回复
::FFFF:d.d.d
::FFFF:d.d
::d.d.d
::d.d
上述地址是无效的,还有
::FFFF:d
::d
虽是有效的,但易与 IPv6 混淆,属于非常规的格式,在转换时一律看作是 IPv6 格式。
若为非正常的 IPv6 地址,在 toStandarIPv6() 方法中会产生异常,不知是否能够捕获异常来判断。
至于,正则表达式验证较为复杂,我暂时无法完成,肯请高人帮忙。
以下是更改后的代码段
public class IPv6 {
public static void main(String[] args) {
String ipv6 = "41:3::4:FFFF:210.147.158.1";
System.out.println(checkIPv6(ipv6));
ipv6 = toStandarIPv6(ipv6);
System.out.println(ipv6);
short[] s = ipv6ToShort(ipv6);
byte[] b = ipv6ToByte(ipv6);
for (short c : s) {
System.out.printf("%9d ", c);
}
System.out.println();
for (byte c : b) {
System.out.printf("%4d ", c);
}
System.out.println();
}
// 转换成标准格式的 IPv6
public static String toStandarIPv6(String ipv6) {
ipv6 = ipv6.trim();
String[] ipv6s = ipv6.split(":");
StringBuffer ip6 = new StringBuffer();
for(int i=0; i<ipv6s.length; i++){
if(ipv6s[i].length()!=0){
while(ipv6s[i].length()<4){
ipv6s[i] = "0" + ipv6s[i];
}
}
ip6.append(ipv6s[i]).append(":");
}
ipv6 = ip6.toString().substring(0, ip6.length()-1);
// 是否带有 IPv4 后缀
int pre = (ipv6.indexOf('.') < 0) ? 8 : 7;
// 补足 IPv6 中被压缩的部分
if (ipv6s.length < pre) {
StringBuffer sb = new StringBuffer();
if (ipv6.indexOf("::") != 0) {
sb.append(":");
} else {
sb.append("0000:");
}
for (int i = ipv6s.length; i <= pre; i++) {
sb.append("0000:");
}
ipv6 = ipv6.replace("::", sb.toString());
}
if (pre == 7) {
// 将后缀的 4 位 IPv4,转为 2 位的 IPv6
String[] ipv4 = ipv6s[ipv6s.length - 1].split("\\.");
StringBuffer sb = new StringBuffer();
for (int i = 0; i < ipv4.length / 2; i++) {
sb.append(":");
String hex = Integer.toHexString(Integer.parseInt(ipv4[2 * i]));
hex = (hex.length() < 2) ? "0" + hex : hex;
sb.append(hex);
hex = Integer.toHexString(Integer.parseInt(ipv4[2 * i + 1]));
hex = (hex.length() < 2) ? "0" + hex : hex;
sb.append(hex);
}
ipv6 = ipv6.substring(0, ipv6.lastIndexOf(":"));
ipv6 = ipv6 + sb.toString();
}
return ipv6.toUpperCase();
}
// 将IPv6转为byte[16],大于 0x7F 的值用负数表示
public static byte[] ipv6ToByte(String ipv6) {
byte[] ipv6Byte = new byte[16];
String[] ipv6s = ipv6.split(":");
for (int i = 0; i < ipv6Byte.length / 2; i++) {
int k = Integer.parseInt(ipv6s[i], 16);
ipv6Byte[2 * i] = (byte) (k >> 8);
ipv6Byte[2 * i + 1] = (byte) (k & 0xff);
}
return ipv6Byte;
}
// 将IPv6转为short[8],大于 0x7FFF 的值用负数表示
public static short[] ipv6ToShort(String ipv6) {
short[] ipv6Short = new short[8];
String[] ipv6s = ipv6.split(":");
for (int i = 0; i < ipv6Short.length; i++) {
ipv6Short[i] = (short) Integer.parseInt(ipv6s[i], 16);
}
return ipv6Short;
}
public static boolean checkIPv6(String ipv6) {
// 验证暂时无法完成,肯请高手帮忙
return true;
}
}
taotiede1
2007-07-03
打赏
举报
回复
学习一下
scvzhang
2007-07-03
打赏
举报
回复
bao110908(bao)(bao)
辛苦了。
学习中
rainv
2007-07-02
打赏
举报
回复
顶
angelseyes
2007-07-02
打赏
举报
回复
学习,顶
ker79
2007-07-02
打赏
举报
回复
学习
火龙果被占用了
2007-07-02
打赏
举报
回复
public class IPv6 {
public static void main(String[] args) {
String ipv6 = "3255:0304:0000:FE4A:174F:5577:289C:0014";
short[] s = ipv6ToShort(ipv6);
byte[] b = ipv6ToByte(ipv6);
for (short c : s) {
System.out.printf("%9d ", c);
}
System.out.println();
for (byte c : b) {
System.out.printf("%4d ", c);
}
System.out.println();
System.out.println(checkIPv6(ipv6));
}
// 将 IPv6 转为 byte[16],大于 0x7F 的值用负数表示
public static byte[] ipv6ToByte(String ipv6) {
byte[] ipv6Byte = new byte[16];
String[] ipv6s = ipv6.split(":");
for (int i = 0; i < ipv6Byte.length / 2; i++) {
int k = Integer.parseInt(ipv6s[i], 16);
ipv6Byte[2 * i] = (byte) (k >> 8);
ipv6Byte[2 * i + 1] = (byte) ((k | 0xff00) & 0xff);
}
return ipv6Byte;
}
// 将 IPv6 转为 short[8],大于 0x7FFF 的值用负数表示
public static short[] ipv6ToShort(String ipv6) {
short[] ipv6Short = new short[8];
String[] ipv6s = ipv6.split(":");
for (int i = 0; i < ipv6Short.length; i++) {
ipv6Short[i] = (short)Integer.parseInt(ipv6s[i], 16);
}
return ipv6Short;
}
// 检验 IPv6 的格式
public static boolean checkIPv6(String ipv6) {
return ipv6.matches("(\\p{XDigit}){1,4}(:(\\p{XDigit}){1,4}){7}");
}
}
scvzhang
2007-07-02
打赏
举报
回复
bao110908(bao)(bao)
还有特殊的IPV6格式。下面是JDK里的IPV6事例,程序没有考虑。
1080:0:0:0:8:800:200C:417A
1080::8:800:200C:417A
::FFFF:129.144.52.38
::129.144.52.38
::FFFF:d.d.d
::FFFF:d.d
::d.d.d
::d.d
::FFFF:d
闲谈
IPv6
-
IPv6
的分片(
IPv6
Fragment)
熟悉IPv4的肯定知道IP分片这个特性,它在某种意义上让应用程序忘记了数据包还有大小这个属性,也就是说,应用程序可以发送小于IP头规定的最长65535字节的任意大小的数据包。 IPv4严格采纳分层模型,让路径MTU这种事做到对应用程序完全透明而无感知。如果路径MTU太小不足以让大数据报文通过,那么 分片 这种机制便开始起作用。 IP分片是自动进行的,假设有下面的IP报文序列要发送: P1-P2-P...
Linux
IPv6
| 地址配置 /
IPv6
禁用 /
IPv6
排障
……
getaddrinfo
函数
ipv6
的应用
1..通过域名转换为点分十进制
ipv6
地址 addrinfo转换为sockaddr_in6 2.程序 #include #include #include #include #include #include #include int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "U
IPV6
改造
网络邻居 假设在 IP 为 fe80::14de:fd3b:94c7:09dc 的电脑上共享了一个叫做 share 的目录,要访问该目录,首先要将IP地址中的冒号“:” 替换为短横线“-” ,然后再添加后缀 .
ipv6
-literal.net 。转换后的
IPV6
称为 UNC 格式 。因此需要使用如下路径访问 share 目录: //fe80–14de-fd3b-94c7-09dc .
ipv6
-l...
IPv6
协议解析、
IPv6
静态路由配置、
IPv6
动态路由之OSPFv3配置
IPv6
原理及配置
Java SE
62,620
社区成员
307,251
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章