6 Replies Latest reply: Feb 20, 2012 3:20 PM by 918025 RSS

    Java & Windows (7): Reliably getting IPv4 netmask?

    918025
      I've run into a known bug with Java 6 on Windows. My understanding is that the normal way to get the netmask is to look up the network prefix length and do some bit shifts. The problem is that on Windows the prefix length is often returned incorrectly, so we get a 128 when we should get a 24 or 20.

      At http://stackoverflow.com/questions/1221517/how-to-get-subnet-mask-using-java, it is suggested to put -Djava.net.preferIPv4Stack=true on the Java command line. Unfortunately, on Windows 7, adding that as either a VM parameter or on the Java command line seems to have no effect.

      (a) Does anyone know any OTHER work-arounds for this problem that might still work on Windows 7?

      (b) Alternatively, is there an entirely different way to get the netmask that is reliable?

      Thanks!
        • 1. Re: Java & Windows (7): Reliably getting IPv4 netmask?
          796440
          Crosspost.

          http://www.coderanch.com/t/567601/sockets/java/Java-Windows-Reliably-getting-IPv
          • 2. Re: Java & Windows (7): Reliably getting IPv4 netmask?
            918025
            On CodeRanch, someone explained that I had made an etiquette error by not listing the other places where I have asked this question.

            Here they are:
            http://stackoverflow.com/questions/9299566/java-windows-7-reliably-getting-ipv4-netmask
            http://www.codeguru.com/forum/showthread.php?t=521196
            Java & Windows (7): Reliably getting IPv4 netmask?
            http://www.coderanch.com/t/567601/sockets/java/Java-Windows-Reliably-getting-IPv

            I am actively monitoring each site.

            Explanation: I first posted on StackOverflow, but I didn't get a response. Usually I'm amazed at how quick people are to respond on that site, so I'm assuming the lack of response is due to this being a hard problem. I've been digging pretty hard on this, and most I've seen is some suggestions as to how to fix the problem in Java itself, but no work-arounds that actually work for me. Thus, I was motivated to post in multiple places in the hope that I'd get some help somewhere.

            Sorry for the confusion.

            Edited by: 915022 on Feb 16, 2012 8:55 AM

            Edited by: 915022 on Feb 16, 2012 9:01 AM
            • 3. Re: Java & Windows (7): Reliably getting IPv4 netmask?
              gimbal2
              915022 wrote:
              Actually, I first posted on StackOverflow, but I didn't get a response. This is probably a hard problem, so I'm not surprised. I hope OTN doesn't have a rule against cross-posting.
              Not that strict, but if you do crosspost for whatever reason, post a link to your original post so people can follow it and check if an answer has already been given. Otherwise you are just wasting people's time.

              EDIT: yep, ninja'd.
              • 4. Re: Java & Windows (7): Reliably getting IPv4 netmask?
                jtahlborn
                when i run the code you linked to from stackoverflow on my windows 7 box, i get the correct answer. specifically, i ran this test code:
                        InetAddress localHost = Inet4Address.getLocalHost();
                        NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localHost);
                        System.out.println("Localhost len " + networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength());
                
                        for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                            System.out.println("Addr " + address);
                            InetAddress addr = address.getAddress();
                            System.out.println("Is ipv4? " + (addr instanceof Inet4Address));
                            System.out.println("Prefix len " + address.getNetworkPrefixLength());
                        }
                • 5. Re: Java & Windows (7): Reliably getting IPv4 netmask?
                  918025
                  And in fact, on my WinXP machine, it works fine too. But here's some debug output from my program running on Win 7:
                  16 /fe80:0:0:0:4b6:941b:9c65:6797%22 fe80:0:0:0:4b6:941b:9c65:6797%22 64
                  4 /10.1.2.40 10.1.2.40 128
                  Printed by this
                          for (InterfaceAddress address : networkInterface.getInterfaceAddresses()) {
                              System.out.print(address.getAddress().getAddress().length + " ");
                              System.out.print(address.getAddress() + " ");
                              System.out.print(address.getAddress().getHostAddress() + " ");
                              System.out.println(address.getNetworkPrefixLength());
                          }
                  The first line is the IPv6 address, and it yields a prefix length of 64. No idea if that's right. The second line is the IPv4 address. Notice the bogus length of 128.

                  Here's the actual bug report: [url http://bugs.sun.com/view_bug.do?bug_id=6707289]http://bugs.sun.com/view_bug.do?bug_id=6707289

                  Edited by: 915022 on Feb 16, 2012 10:06 AM
                  • 6. Re: Java & Windows (7): Reliably getting IPv4 netmask?
                    918025
                    This answer worked. I needed to put this line of code first in my main method:

                    System.setProperty("java.net.preferIPv4Stack","true");

                    I don't know why putting it on the command line didn't take, but this worked.