Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6223936

REGRESSION: method java.net.InetAddress.toString() ignores the hostname under certain circumstances

XMLWordPrintable

    • generic
    • generic

      According to the java.net.InetAddress.getAllByName(String) API, the host name can either be a machine name or a textual representation of its IP address.

      If you specify a textual representation of an IP address, the java.net.InetAddress.toString() method ignores the hostname since J2SE 1.4.2. The output produced by the following application will demonstrate this regression.

      % cat Addr.java
      import java.net.*;

      public class Addr {
        public static void main(String args[]) {
            InetAddress addrs[] = null;
            try {
                addrs = InetAddress.getAllByName(args[0]);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }

            for (int i = 0; i < addrs.length; i++) {
                System.out.println(addrs[i]);
            }
        }
      }

      See the comments section for an output of the program.
      With J2SE 1.2.2 and J2SE 1.3.1 the addrs[i].toString() returns both hostname and IP-address. Since J2SE 1.4.2 we only get the IP-address, the hostname is not present.

      If you change

      System.out.println(addrs[i]);

      to

      System.out.println(addrs[i].getHostName()+"/"+addrs[i].getHostAddress());

      in the snippet above, it also works with 1.4.2_x and 1.5.0_y.

      ###@###.### 2005-2-01 09:32:15 GMT

            npatelsunw Nishant Patel (Inactive)
            jloefflm Johann Löfflmann (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: