I made a class that resolve a domain to a ip, now i need this app to show me both ipv4 and ipv6 but it only shows me ipv4. The problems is i think in this line of the code :
InetAddress[] addresses = InetAddress.getAllByName(query);
System.out.println(Inet6Address.getByName(query));
This line : InetAddress[] addresses = InetAddress.getAllByName(query); returns only the ip4 adresses even if i se the Inet6Address instead of InetAddress
package agin;
import java.*;
import java.util.*;
public class DNSClient {
private static String dnsServer = null;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("> ");
String input = scanner.nextLine().trim();
String[] parts = input.split(" ");
if (parts.length == 0) continue;
if (parts[0].equalsIgnoreCase("resolve")) {
if (parts.length != 2) {
System.out.println("Utilizare: resolve <domain> sau resolve <ip>");
continue;
}
resolve(parts[1]);
} else if (parts[0].equalsIgnoreCase("use") && parts.length == 3 && parts[1].equalsIgnoreCase("dns")) {
dnsServer = parts[2];
if (!testDNS(dnsServer)) {
System.out.println("Eroare: DNS server invalid.");
dnsServer = null;
} else {
System.out.println("DNS server setat la: " + dnsServer);
}
} else {
System.out.println("Comanda necunoscuta!");
}
}
}
private static void resolve(String query) {
try {
InetAddress[] addresses = InetAddress.getAllByName(query);
System.out.println(Inet6Address.getByName(query));
List<String> ipv4List = new ArrayList<>();
List<String> ipv6List = new ArrayList<>();
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
ipv6List.add(addr.getHostAddress());
} else {
ipv4List.add(addr.getHostAddress());
}
}
System.out.println("Rezultate pentru " + query + ":");
if (!ipv4List.isEmpty()) {
System.out.println("Adrese IPv4:");
ipv4List.forEach(ip -> System.out.println(" - " + ip));
}
if (!ipv6List.isEmpty()) {
System.out.println("Adrese IPv6:");
ipv6List.forEach(ip -> System.out.println(" - " + ip));
}
} catch (UnknownHostException e) {
System.out.println("Nu s-a putut rezolva: " + query);
}
}
private static boolean testDNS(String dns) {
try {
Process process = Runtime.getRuntime().exec("nslookup google " + dns);
process.waitFor();
return process.exitValue() == 0;
} catch (Exception e) {
return false;
}
}
}
To run the app compile run and write : resolve google
I made a class that resolve a domain to a ip, now i need this app to show me both ipv4 and ipv6 but it only shows me ipv4. The problems is i think in this line of the code :
InetAddress[] addresses = InetAddress.getAllByName(query);
System.out.println(Inet6Address.getByName(query));
This line : InetAddress[] addresses = InetAddress.getAllByName(query); returns only the ip4 adresses even if i se the Inet6Address instead of InetAddress
package agin;
import java.*;
import java.util.*;
public class DNSClient {
private static String dnsServer = null;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("> ");
String input = scanner.nextLine().trim();
String[] parts = input.split(" ");
if (parts.length == 0) continue;
if (parts[0].equalsIgnoreCase("resolve")) {
if (parts.length != 2) {
System.out.println("Utilizare: resolve <domain> sau resolve <ip>");
continue;
}
resolve(parts[1]);
} else if (parts[0].equalsIgnoreCase("use") && parts.length == 3 && parts[1].equalsIgnoreCase("dns")) {
dnsServer = parts[2];
if (!testDNS(dnsServer)) {
System.out.println("Eroare: DNS server invalid.");
dnsServer = null;
} else {
System.out.println("DNS server setat la: " + dnsServer);
}
} else {
System.out.println("Comanda necunoscuta!");
}
}
}
private static void resolve(String query) {
try {
InetAddress[] addresses = InetAddress.getAllByName(query);
System.out.println(Inet6Address.getByName(query));
List<String> ipv4List = new ArrayList<>();
List<String> ipv6List = new ArrayList<>();
for (InetAddress addr : addresses) {
if (addr instanceof Inet6Address) {
ipv6List.add(addr.getHostAddress());
} else {
ipv4List.add(addr.getHostAddress());
}
}
System.out.println("Rezultate pentru " + query + ":");
if (!ipv4List.isEmpty()) {
System.out.println("Adrese IPv4:");
ipv4List.forEach(ip -> System.out.println(" - " + ip));
}
if (!ipv6List.isEmpty()) {
System.out.println("Adrese IPv6:");
ipv6List.forEach(ip -> System.out.println(" - " + ip));
}
} catch (UnknownHostException e) {
System.out.println("Nu s-a putut rezolva: " + query);
}
}
private static boolean testDNS(String dns) {
try {
Process process = Runtime.getRuntime().exec("nslookup google " + dns);
process.waitFor();
return process.exitValue() == 0;
} catch (Exception e) {
return false;
}
}
}
To run the app compile run and write : resolve google
Share Improve this question asked Mar 6 at 12:56 Lungu MihaiLungu Mihai 11 bronze badge 10 | Show 5 more comments1 Answer
Reset to default 0So you'll want to make sure this is not set anywhere: -Djava.preferIPv4Stack=true
that property forces Java to use IPV4 stack and won't use IPV6. If you are running your code inside an app server like Tomcat or Jetty sometimes this setting might exist. Generally tomcat and jetty don't have this as a default, but it possible someone else could have configured it. And if you are using some other system it might be a default so it's worth checking it.
Try running this in the same environment your code is running to look at your system properties:
class Scratch {
public void printSystemProperties() {
System.out.println("System Properties");
System.getProperties().forEach((key,value)->{
System.out.println(key+"="+value);
});
}
public static void main(String[] args) {
new Scratch().printSystemProperties();
}
}
dig google
ornslookup google
on a command line, depending on what is available in your OS. – aled Commented Mar 6 at 13:25Inet6Address.getByName("google")
indeed yields an IPv4 on my machine too. But printing the class of each element in the result ofInetAddress.getAllByName("google")
(as well as running the code) yields both IPv4 and IPv6 addresses. At least on my machine, so I cannot reproduce what you are seeing. By the waygetByName
method is actually defined insideInetAddress
(no need for calling it fromInet6Address
instead). – gthanop Commented Mar 6 at 13:32getByName
exists inInetAddress
and since it is a static method, then can be called by subclasses too (such asInet6Adress.getByName
, that you did), but I just clarified this in order to avoid any possible confusion thatInet6Address.getByName
returns always only IPv6 addresses, as I assumed one might think. And yes,InetAddress.getByName
andInet6Adress.getByName
should always return the exact same result, since it is the same method call, but don't think thatInet6Address.getByName
should return IPv6 addresses only. – gthanop Commented Mar 6 at 21:02