java服务端升级ipv6,在Java中返回IPv6

Is there a way in Java to tell it to return IPv6 only? I've tried everything and can't get it to work.

try

{

InetAddress inet = InetAddress.getByName(hostName);

boolean status = inet.isReachable(5000);

if (status)

{

System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());

}

else

{

System.out.println(inet.getCanonicalHostName() + " Host Unreachable");

}

}

catch (UnknownHostException e)

{

System.err.println("Host does not exists");

}

catch (IOException e)

{

System.err.println("Error in reaching the Host");

}

The line I use to try to return IPv6 only:

System.out.println(inet.getCanonicalHostName() + " Host Reached\t" + java.net.Inet6Address.getByName(hostName).getHostAddress());

This keeps returning IPv4. Anyone have any idea of why its doing this?

解决方案

java.net.Inet6Address does not override getByName()

so it will always return the specific IPv4-Address,

unless your parameter itself is in the form of an valid IPv6-Address, in this case this method will return an Inet6Address-Object.

For example:

getByName("stackoverflow.com") --> Inet4Address

getByName("2001:0db8:85a3:08d3:1319:8a2e:0370:7344") --> Inet6Address

InetAddress.getByName()-Documentation

Determines the IP address of a host, given the host's name. The host name can either be a machine name, such as "java.sun.com", or a

textual representation of its IP address. If a literal IP address is

supplied, only the validity of the address format is checked.

> For host specified in literal IPv6 address, either the form defined in

RFC 2732 or the literal IPv6 address format defined in RFC 2373 is

accepted.<

So if you want to get an IPv6-Address you need to define it within your parameter, or configure a DNS-Server to return the IPv6-Address instead of the IPv4-Address.

Another way to retrieve the IPv6-Address is using InetAddress.getAllByName("www.google.at") which returns all known IP-Addresses of the host.

For example you can use this method to filter the returned array, which return the first IPv6-Address or null if the host don't have one:

public Inet6Address getIPv6Addresses(InetAddress[] addresses) {

for (InetAddress addr : addresses) {

if (addr instanceof Inet6Address) {

return (Inet6Address) addr;

}

}

return null;

}

UPDATE:

For more functions, especially those affecting DNS-Servers, I recommend using the external library DNSJava, because the plain Java implementation of DNS support is poor.

http://www.dnsjava.org/

Current Code:

public class Ping

{

public void pingHost (String hostName)

{

try

{

InetAddress[] inet = InetAddress.getAllByName(hostName);

String address = this.getIPv4Addresses(inet).getHostAddress();

boolean status = this.getIPv6Addresses(inet).isReachable(5000);

if (status)

{

System.out.println(reverseDns(address) + " Host Reached\t" + this.getIPv6Addresses(inet).getHostAddress());

}

else

{

System.out.println(this.getIPv6Addresses(inet).getCanonicalHostName() + " Host Unreachable");

}

}

catch (UnknownHostException e)

{

System.err.println("Host does not exists");

}

catch (IOException e)

{

System.err.println("Error in reaching the Host");

}

}

public Inet6Address getIPv6Addresses(InetAddress[] addresses)

{

for (InetAddress addr : addresses)

{

if (addr instanceof Inet6Address)

{

return (Inet6Address) addr;

}

}

return null;

}

public Inet4Address getIPv4Addresses(InetAddress[] addresses)

{

for (InetAddress addr : addresses)

{

if (addr instanceof Inet4Address)

{

return (Inet4Address) addr;

}

}

return null;

}

public static String reverseDns(String hostIp) throws IOException

{

Resolver res = new ExtendedResolver();

Name name = ReverseMap.fromAddress(hostIp);

int type = Type.PTR;

int dclass = DClass.IN;

Record rec = Record.newRecord(name, type, dclass);

Message query = Message.newQuery(rec);

Message response = res.send(query);

Record[] answers = response.getSectionArray(Section.ANSWER);

if (answers.length == 0)

return hostIp;

else

return answers[0].rdataToString();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值