3

I used NSDServiceInfo.getHost (). getHostAddress () output is the IPv4 to IPv6 device and router. for the device, I can already IPV4 so I need IPv6. as well as to the router, I can already so I need IPV4 IPV6.

The solution I have tried. but when his hostaddress issued IPV4, then exit the error "Not an IPv6 address: [xx, xx, xx, xx]

Meanwhile, when it issued hostaddress IPv6, then the error message "java.net.Inet6Address can not be cast to java.net.Inet4Address"

I like this coding

List <NSDServiceInfo> Data = new ArrayList<>();
InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress());
byte [] addressBytes = hostInet.getAddress();

Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet));
Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes);
Log.d ( "IP", "IPv4" + dest4.getHostAddress ());
Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());

3 Answers3

2

First fetch the InetAddress object using static function by giving host name

InetAddress hostInet= InetAddress.getByName(hostNameString);

Now we need the byte array representation using hostInet

byte [] addressBytes = hostInet.getAddress();

Now you can use this addressBytes to format is as IPv4 or IPv6 using Inet6Address or Inet4Address with getByAddress plus you need to pass hostName , bytes address and network interface using getByInetAddress function

Inet6Address dest = Inet6Address.getByAddress(hostNameString, addressBytes, NetworkInterface.getByInetAddress(hostInet));

or

Inet4Address dest = Inet4Address.getByAddress(hostNameString, addressBytes, NetworkInterface.getByInetAddress(hostInet));

Now you can retrieve either text or byte representation of dest object using getAddress or getHostAddress functions

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

this my fault for not complete in asking. so I used NSDServiceInfo.getHost (). getHostAddress () output is the IPv4 to IPv6 device and router. for the device, I can already IPV4 so I need IPv6. as well as to the router, I can already so I need IPV4 IPV6.

The solution I have tried. but when his hostaddress issued IPV4, then exit the error "Not an IPv6 address: [xx, xx, xx, xx]

Meanwhile, when it issued hostaddress IPv6, then the error message "java.net.Inet6Address can not be cast to java.net.Inet4Address"

I like this coding

List <NSDServiceInfo> Data = new ArrayList<>();
InetAddress hostInet =InetAddress.getByName(Data.get(position).getHost().GetHostAddress());
byte [] addressBytes = hostInet.getAddress();

Inet6Address dest6 = Inet6Address.getByAddress(Data.get(position).getHost().GetHostAddress(), addressBytes, NetworkInterface.getByInetAddress(hostInet));
Inet4Address dest4 = (Inet4Address) Inet4Address.getByAddress (Data.get(position).getHost().GetHostAddress(), addressBytes);
Log.d ( "IP", "IPv4" + dest4.getHostAddress ());
Log.d ( "IP", "IPv6:" + dest6.getHostAddress ());

sorry if my English is bad thank you

0

Class NsdServiceInfo has a method getHostAddress() that is a type of java.net.InetAddress. And the textual representation of an IP address is address family specific:

Both these classes extend InetAddress. You need a method public String getHostAddress() that returns the IP address string in textual presentation.

DimaSan
  • 12,264
  • 11
  • 65
  • 75