最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ip - Java does not want to return IPV6, only IPV4 when try to resolve an DNS - Stack Overflow

programmeradmin2浏览0评论

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
  • 1 How do you know it is a problem with your program and not a DNS response? Try dig google or nslookup google on a command line, depending on what is available in your OS. – aled Commented Mar 6 at 13:25
  • @aled if I run the code locally, I also only get the ip4 results, while an nslookup in a terminal also returns a couple of ip6 addresses. – Stultuske Commented Mar 6 at 13:30
  • 1 Printing Inet6Address.getByName("google") indeed yields an IPv4 on my machine too. But printing the class of each element in the result of InetAddress.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 way getByName method is actually defined inside InetAddress (no need for calling it from Inet6Address instead). – gthanop Commented Mar 6 at 13:32
  • @Lungu maybe you can take a look at this. If I run this for google, I get the entire list. coderanch/t/371533/java/nslookup – Stultuske Commented Mar 6 at 13:38
  • 1 As for the other point I tried to make, getByName exists in InetAddress and since it is a static method, then can be called by subclasses too (such as Inet6Adress.getByName, that you did), but I just clarified this in order to avoid any possible confusion that Inet6Address.getByName returns always only IPv6 addresses, as I assumed one might think. And yes, InetAddress.getByName and Inet6Adress.getByName should always return the exact same result, since it is the same method call, but don't think that Inet6Address.getByName should return IPv6 addresses only. – gthanop Commented Mar 6 at 21:02
 |  Show 5 more comments

1 Answer 1

Reset to default 0

So 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();
    }
}
发布评论

评论列表(0)

  1. 暂无评论