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

PlainSocketImpl.socketAccept incorrectly fills in addr/port for remote socket

XMLWordPrintable

    • 1.2alpha
    • sparc
    • solaris_2.5
    • Not verified

      In JDK 1.2, if you get a Socket returned from ServerSocket.accept and call
      getInetAddress to get the address of the remote peer, it will return 0.0.0.0,
      and if you try to get the port on the remote peer, it will return the local
      port on which the ServerSocket was listening.

      The bug is due to an error in the platform specific native code for the
      java.net.PlainSocketImpl class, although the same error is in both the solaris
      and win32 implementations.

      The cause of the bug is a few typos from when the PlainSocketImpl class's
      native methods were converted to use the JNI. At the end of
      Java_java_net_PlainSocketImpl_socketAccept, it writes to the InetAddress of
      the ServerSocket (this) instead of accepted socket (socket), and it writes
      the ServerSocket's local port into the "port" field of the client Socket
      (overwriting what had been correctly put there before) instead of the
      "localport" field.

      The example class LocalTest given below demonstrates the problem; it says
      that the client's address is "0.0.0.0" when it should be "127.0.0.1". It
      also says that the client's remote port is 2001 (which is really its local
      port) and that its local port is 0.


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

      public class LocalTest implements Runnable {

          private static final int PORT = 2001;

          private ServerSocket server;

          public LocalTest(ServerSocket server) {
              this.server = server;
          }

          public void run() {
              try {
                  Socket socket = server.accept();
                  System.out.println("Accepted socket: " + socket);
                  InetAddress addr = socket.getInetAddress();
                  System.out.println("Client's address: " + addr);
              } catch (IOException e) {
                  System.err.println("I/O EXCEPTION OCCURRED:");
                  e.printStackTrace();
              }
          }

          public static void main(String[] args) {
              try {
                  ServerSocket server = new ServerSocket(PORT);
                  System.out.println("Created server socket: " + server);
                  (new Thread(new LocalTest(server))).start();
                  Thread.sleep(2000);
                  Socket socket = new Socket("", PORT);
                  System.out.println("Create client socket: " + socket);
              } catch (Exception e) {
                  System.err.println("EXCEPTION OCCURRED:");
                  e.printStackTrace();
              }
          }
      }

            busersunw Btplusnull User (Inactive)
            peterjones Peter Jones (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: