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

Socket.getLocalAddress() returns incorrect byte order

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.1.5
    • core-libs
    • x86
    • solaris_2.6



      Name: dgC58589 Date: 01/20/98


      The InetAddress returned by Socket.getLocalAddress
      returns the IP address in a reverse order.
      The bug is present on Solaris x86 1.1.5 and
      all tested versions of the Linux JDK.
      The sparc version works correctly.

      Source code follows:

      import java.net.*;
      import java.io.*;

      public
      class inet
      {
        public static void main(String argv[])
        {
          if (argv.length < 2)
          {
            System.err.println("Usage : java inet <host> <port>");
            System.exit(-1);
          }

          int port = 0;
          String host = argv[0];

          try
          {
            port = Integer.parseInt(argv[1]);
          }
          catch (NumberFormatException e)
          {
            System.err.println("<port> must be a number!");
            System.err.println("Usage : java inet <host> <port>");
            System.exit(-1);
          }

          try
          {
            Socket sock = new Socket(host,port);
            InetAddress in = sock.getLocalAddress();
            InetAddress in2 = InetAddress.getByName(host);

            System.out.println("Socket.getLocalAddress returns (wrong) : " + in);
            System.out.println("InetAddress.getByName returns (right) : " + in2);
          }
          catch (UnknownHostException e1)
          {
            System.err.println(e1);
            System.err.println("Usage : java inet <host> <port>");
            System.exit(-1);
          }
          catch (IOException e2)
          {
            e2.printStackTrace(System.err);
          }

        }
      }
      (Review ID: 23574)
      ======================================================================

            brenaudsunw Benjamin Renaud (Inactive)
            dgrahamcsunw David Graham-cumming (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: